Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How perform task on JavaFX TextField at onfocus and outfocus?

I am working on JavaFX project. I need to perform some task on a JavaFX TextField.

For example on the "on focus" event for the TextField I want to print

System.out.println("Textfield on focus"); 

and on the "out focus" event it should print

System.out.println("Textfield out focus"); 
like image 305
java baba Avatar asked May 14 '13 17:05

java baba


1 Answers

I thought it might be helpful to see an example which specifies the ChangeListener as an anonymous inner class like scottb mentioned.

TextField yourTextField = new TextField(); yourTextField.focusedProperty().addListener(new ChangeListener<Boolean>() {     @Override     public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)     {         if (newPropertyValue)         {             System.out.println("Textfield on focus");         }         else         {             System.out.println("Textfield out focus");         }     } }); 

I hope this answer is helpful!

like image 154
Brendan Avatar answered Oct 02 '22 11:10

Brendan