- Use an
ActionListener
instead of aKeyListener
andKeyEvent.VK_ENTER
- lots of reasons why, but basically, this is what anActionListener
was designed to do. - Use a class field instead of a local variable...
More like...
public class MyForm extends JPanel {
private JTextField typingArea;
private String userWord;
public MyForm() {
typingArea = new JTextField();
typingArea.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
userWord = typingArea.getText().toLowerCase();
typingArea.setText("");
}
});
}
}