Easy way to cancel user input on esc key?

Is there an easy way to cancel user input in a JTextField when pressing the Esc key ?

I mean something else that is a key listener and data backup.

thanks

+4
source share
2 answers

Add a KeyListener to your JTextField:

JTextField field = new JTextField.addKeyListener(yourKeyListener); 

where yourKeyListener could be:

 public class YourKeyListener implements KeyListener{ void keyPressed(KeyEvent e){ Component source = e.getSource(); if (source instanceof JTextField && e.getId() == KeyEvent.VK_ESCAPE ){ JTextField f = (JTextField) source; f.setText(""); } } } 
+1
source

CancelAction , discussed here and shown here , is an example that uses Action and key binding .

0
source

All Articles