How perform task on JavaFX TextField at onfocus and outfocus?

JavaJavafx 2JavafxJavafx 8

Java Problem Overview


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");

Java Solutions


Solution 1 - Java

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!

Solution 2 - Java

You can use the focusedProperty of Node to attach a ChangeListener.

Extending the answer from Brendan: from JavaFX8 (as it comes with Java 8), a lambda expression combined with a ternary operator can make it really compact:

textField.focusedProperty().addListener((obs, oldVal, newVal) -> 
    System.out.println(newVal ? "Focused" : "Unfocused"));

Solution 3 - Java

You will want to attach a ChangeListener to the FocusProperty of the TextField that you wish to monitor.

In JavaFX, you can attach notification events (Change or Invalidation Listeners) to any JavaFX property that an object may possess as long as the property meets the minimum definition for a JavaFX bean.

Refer to this post if your event handlers will be doing other things such as modifying Cancel or Default button settings: https://stackoverflow.com/questions/16375967/javafx-2-setting-the-defaultbutton-property-mutually-exclusive

Here is some code to attach a Change Listener to a text box:

txtDx.focusedProperty().addListener(m_txtDxListener);

The Listener object has been stored in an instance field so that it may be used with both addListener() and removeListener(). For a short lived TextField, you can specify the listener object with an anonymous inner class.

Here is the private class that I wrote for my focus listener:

private class FocusPropertyChangeListener implements ChangeListener<Boolean> {
    
    FocusPropertyChangeListener() { System.out.println("New FPCL instance"); }
    
    @Override
    public void changed(ObservableValue<? extends Boolean> ov, 
        Boolean oldb, Boolean newb) {
        System.out.println("Focus change triggered");

        if (ancEncEditor.isVisible() && !ancEncEditor.isDisabled()) {
            boolean b = (newb != null && newb.booleanValue() == true);
            System.out.println("txtDx focus change event triggered: DxAdd = " + b);
            
            if (b) { btnDxAdd.setDefaultButton(true); }
            else { btnWindowCommit.setDefaultButton(true); }
            btnWindowCommit.setCancelButton(true);
            btnDxAdd.setDefaultButton(b);
        }
    }
}

Solution 4 - Java

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");
        }
    }
});

or you can use in java 8 or above

textField.focusedProperty().addListener((obs, oldVal, newVal) -> 
    System.out.println(newVal ? "textfield on focus" : "Textfield out focus"));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjava babaView Question on Stackoverflow
Solution 1 - JavaBrendanView Answer on Stackoverflow
Solution 2 - JavaDVargaView Answer on Stackoverflow
Solution 3 - JavascottbView Answer on Stackoverflow
Solution 4 - JavanezarView Answer on Stackoverflow