James James - 6 months ago 17
Java Question

Issue with KeyListener

I have a small class that implements KeyListener, but even though I implemented all methods, including keyPressed(), and gave them all code to run, nothing happens when I press the enter (return) key.

Here is the class that I made implement KeyListener (the implemented methods are at the bottom):

public class TestPanel extends JPanel implements KeyListener {
Words word = new Words();
private JLabel rootLabel;
JLabel rootField;
private JLabel defLabel;
JTextField defField;
private JButton ok;
TestListener listener;
private JLabel correctLabel;
private JLabel incorrectLabel;
private int correctCount = 0;
private int incorrectCount = 0;
ActionListener okListener = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (defField.getText().equals("")) {

} else {

String root = word.getRoot();
String def = word.getDef();
boolean status;
if (defField.getText().equals(word.getDef())) {
correctCount++;
correctLabel.setText("Correct: " + correctCount);
status = true;
} else {
incorrectCount++;
incorrectLabel.setText("Incorrect: " + incorrectCount);
status = false;
}

defField.setText("");
word.setRootAndDef();
rootField.setText(word.getRoot());
TestEvent event = new TestEvent(ok, root, def, status);
listener.dataSubmitted(event);

}

}

};

public TestPanel() {
word = new Words();
rootLabel = new JLabel("Root: ");
rootField = new JLabel();
defLabel = new JLabel("Definition: ");
defField = new JTextField(20);
ok = new JButton("OK");
correctLabel = new JLabel("Correct: " + correctCount);
incorrectLabel = new JLabel("Incorrect: " + incorrectCount);

ok.setMnemonic(KeyEvent.VK_O);

setBorder(BorderFactory.createTitledBorder("Test"));

ok.addActionListener(okListener);

setLayout(new GridBagLayout());

GridBagConstraints gc = new GridBagConstraints();

// First row

gc.weightx = 1;
gc.weighty = 0.2;
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.LINE_END;
gc.insets = new Insets(0, 0, 0, 5);

add(rootLabel, gc);

gc.gridx = 1;
gc.anchor = GridBagConstraints.LINE_START;
gc.insets = new Insets(0, 0, 0, 0);

add(rootField, gc);

// Next row

gc.gridx = 0;
gc.gridy = 1;
gc.anchor = GridBagConstraints.LINE_END;
gc.insets = new Insets(0, 0, 0, 5);

add(defLabel, gc);

gc.gridx = 1;
gc.gridy = 1;
gc.anchor = GridBagConstraints.LINE_START;
gc.insets = new Insets(0, 0, 0, 0);

add(defField, gc);

// Next row

gc.weighty = 2;
gc.gridx = 1;
gc.gridy = 2;
gc.anchor = GridBagConstraints.FIRST_LINE_START;

add(ok, gc);

// Next row

gc.weighty = 0.1;
gc.weightx = 1;
gc.gridx = 0;
gc.gridy = 3;
gc.anchor = GridBagConstraints.LINE_END;
gc.insets = new Insets(0, 0, 0, 20);

add(correctLabel, gc);

gc.weighty = 0.1;
gc.weightx = 1;
gc.gridx = 1;
gc.gridy = 3;
gc.anchor = GridBagConstraints.LINE_START;
gc.insets = new Insets(0, 0, 0, 0);

add(incorrectLabel, gc);
}

public void setTestListener(TestListener listener) {
this.listener = listener;
}

public int getCorrectCount() {
return correctCount;
}

public int getIncorrectCount() {
return incorrectCount;
}

public void setCorrectCount(int correctCount) {
this.correctCount = correctCount;
}

public void setIncorrectCount(int incorrectCount) {
this.incorrectCount = incorrectCount;
}

public void setCorrectLabel(String text) {
correctLabel.setText(text);
}

public void setIncorrectLabel(String text) {
incorrectLabel.setText(text);
}

@Override
public void keyPressed(KeyEvent keySource) {

int key = keySource.getKeyCode();

if(key == KeyEvent.VK_ENTER) {
if (defField.getText().equals("")) {

} else {

String root = word.getRoot();
String def = word.getDef();
boolean status;
if (defField.getText().equals(word.getDef())) {
correctCount++;
correctLabel.setText("Correct: " + correctCount);
status = true;
} else {
incorrectCount++;
incorrectLabel.setText("Incorrect: " + incorrectCount);
status = false;
}

defField.setText("");
word.setRootAndDef();
rootField.setText(word.getRoot());
TestEvent event = new TestEvent(ok, root, def, status);
listener.dataSubmitted(event);

}
}

}

@Override
public void keyReleased(KeyEvent keySource) {

int key = keySource.getKeyCode();

if(key == KeyEvent.VK_ENTER) {
if (defField.getText().equals("")) {

} else {

String root = word.getRoot();
String def = word.getDef();
boolean status;
if (defField.getText().equals(word.getDef())) {
correctCount++;
correctLabel.setText("Correct: " + correctCount);
status = true;
} else {
incorrectCount++;
incorrectLabel.setText("Incorrect: " + incorrectCount);
status = false;
}

defField.setText("");
word.setRootAndDef();
rootField.setText(word.getRoot());
TestEvent event = new TestEvent(ok, root, def, status);
listener.dataSubmitted(event);

}
}

}

@Override
public void keyTyped(KeyEvent keySource) {

int key = keySource.getKeyCode();

if(key == KeyEvent.VK_ENTER) {
if (defField.getText().equals("")) {

} else {

String root = word.getRoot();
String def = word.getDef();
boolean status;
if (defField.getText().equals(word.getDef())) {
correctCount++;
correctLabel.setText("Correct: " + correctCount);
status = true;
} else {
incorrectCount++;
incorrectLabel.setText("Incorrect: " + incorrectCount);
status = false;
}

defField.setText("");
word.setRootAndDef();
rootField.setText(word.getRoot());
TestEvent event = new TestEvent(ok, root, def, status);
listener.dataSubmitted(event);

}
}

}

}

Answer

I have a small class that implements KeyListener, but even though I implemented all methods, including keyPressed(), and gave them all code to run, nothing happens when I press the enter (return) key. Please help me with this problem, here is the class that I made implement KeyListener (the implemented methods are at the bottom):

Short answer, don't. If you think you need a KeyListener, the likely hood is you don't. A KeyListener is a low level API which you very rarely need to use, as there are generally better ways to achieve the same thing.

In your case, both the JTextField and JButton support event notification via the ActionListener interface. In the case of the JTextField, this will notify you when the user presses the "action" key while field is focused and in the case of the JButton, when the user presses the "action", the buttons mnemonic key combination or presses the button with the mouse.

The ActionListener API is suppose to remove the complexity involved detecting this functionality and provide you with a simple call back through which you can perform the required action. Let's face it, we don't really care "how" the component was activated, only that it was

Instead, get rid of the KeyListener and simply use something like...

ok.addActionListener(okListener);
defField.addActionListener(okListener);

This way, the user won't need to press the button, but can press the "action" key for the field and the same functionality will get executed.

See How to Use Text Fields, How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listeners for more details

Comments