Java JTextField handle key event
import java.awt.BorderLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class Main extends JFrame { JTextField keyText = new JTextField(80); JLabel keyLabel = new JLabel("Press any key in the text field."); public Main() { super("KeyViewer"); setSize(350, 100);/* ww w . j a va 2 s. c om*/ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); keyText.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent input) { char key = input.getKeyChar(); keyLabel.setText("You pressed " + key); } }); BorderLayout bord = new BorderLayout(); setLayout(bord); add(keyLabel, BorderLayout.NORTH); add(keyText, BorderLayout.CENTER); setVisible(true); } public static void main(String[] arguments) { new Main(); } }