https://bpaste.net/show/09a7053e3805 <-------- LinkList https://bpaste.net/show/
ID: 3765938 • Letter: H
Question
https://bpaste.net/show/09a7053e3805 <-------- LinkList
https://bpaste.net/show/9cb5358e28f6 <--------- Main
I am making this gui of a linked list, the gui to input is working but my legend to let the user know what commands are a loud aren't showing up. it should show up in a seperate window. could someone help me with my issue. or explain what I am doing wrong. I still have to do more to this code. However, I just want to make sure my gui is working as suppose to. Thank you for the help.
Explanation / Answer
You need to add a KeyListener to the TextField not ActionListener. I've bolded the modified code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
/**
* This class is used to demonstrate the operations in the LinkedList1 class.
*/
public class Chp20Pc02Demo extends JFrame {
private final LinkedList1 ll;
private final JTextArea listView;
private final JTextField cmdTextField;
private final JTextField resultTextField;
public Chp20Pc02Demo() {
ll = new LinkedList1();
listView = new JTextArea();
cmdTextField = new JTextField();
resultTextField = new JTextField();
// Create a panel and label for result field
JPanel resultPanel = new JPanel(new GridLayout(1, 2));
resultPanel.add(new JLabel("Command Result"));
resultPanel.add(resultTextField);
resultTextField.setEditable(false);
add(resultPanel, BorderLayout.NORTH);
// Put the textArea in the center of the frame
add(listView);
listView.setEditable(false);
listView.setBackground(Color.WHITE);
// Create a panel and label for the command text field
JPanel cmdPanel = new JPanel(new GridLayout(1, 2));
cmdPanel.add(new JLabel("Command:"));
cmdPanel.add(cmdTextField);
add(cmdPanel, BorderLayout.SOUTH);
cmdTextField.addKeyListener(new CmdTextListener());
// Set up the frame
setTitle("Linked List Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
/**
* Private class that responds to the command that the user types into the
* command entry text field.
*/
private class CmdTextListener implements KeyListener {
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
Scanner sc = new Scanner(cmdText);
String cmd = sc.next();
if (cmd.equals("add")) {
if (sc.hasNextInt()) {
// add index element
int index = sc.nextInt();
String element = sc.next();
ll.add(index, element);
} else {
// add element
String element = sc.next();
ll.add(element);
}
listView.setText(ll.toString());
pack();
}
if (cmd.equals("remove")) {
if (sc.hasNextInt()) {
// remove index
int index = sc.nextInt();
String res = ll.remove(index);
resultTextField.setText(res);
} else {
// remove element
String element = sc.next();
boolean res = ll.remove(element);
String resText = String.valueOf(res);
resultTextField.setText(resText);
}
listView.setText(ll.toString());
pack();
return;
}
if (cmd.equals("isempty")) {
String resText = String.valueOf(ll.isEmpty());
resultTextField.setText(resText);
return;
}
if (cmd.equals("size")) {
String resText = String.valueOf(ll.size());
resultTextField.setText(resText);
}
if (cmd.equals("sort")) {
String resText = String.valueOf(ll.size());
resultTextField.setText(resText);
}
}
}
public void keyReleased(KeyEvent e) {
}
}
/**
* The main method creates an instance of the LinkedList1Demo class which
* causes it to display its window.
*
* @param args
*/
public static void main(String[] args) {
new Chp20Pc02Demo();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.