Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing Call Method Before Focus Change

Tags:

java

swing

I am working with a java swing form that contains a JTextField. The text field needs input validation , validation is performed when the field losses focus . i.e clicking anyware apart from the field itself.

The form also contains a JButton to clear(cancel) the form . Now whenever I press the cancel button and the Textfield is the currently focused component the validation function is invoked. This should not happen.

I have tried InputVerifier and Focuslistener

Is there a way I can know what component caused the focus to change ?

like image 856
Syed Iftekhar Ahmed Avatar asked Feb 01 '26 01:02

Syed Iftekhar Ahmed


1 Answers

This is fine but the form contains a JButton to clear(cancel) the form

You can use:

clearButton.setVerifyInputWhenFocusTarget(false);

to prevent the InputVerifier from being invoked when you click on the button.

like image 65
camickr Avatar answered Feb 02 '26 15:02

camickr