Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I catch the event of exiting a JTextField

currently I use DocumentListener and when I enter "11111"
I get 5 events.

I want 1 event when user exits the field.

like image 992
Bick Avatar asked May 22 '11 14:05

Bick


People also ask

What event does JTextField generate?

When an enter key is pressed in a JTextField, an ActionEvent is generated. In order to handle such event, ActionListener interface should be implemented.

What happens if you press Enter in a J TextField?

If focus is on a JTextField and enter is pressed, the text field fires an ActionEvent.

What is the difference between a JTextField and a JTextArea?

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.


2 Answers

Use a focus listener on the textfield itself.

field.addFocusListener(new FocusAdapter() {
    public void focusLost(FocusEvent e) {
        System.out.println("User entered " + field.getText());
    }
});
like image 66
Howard Avatar answered Sep 29 '22 08:09

Howard


What you need is a FocusListener.

like image 28
Falcon Avatar answered Sep 29 '22 08:09

Falcon