How perform task on JavaFX TextField at onfocus and outfocus?

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!

Leave a Comment