There are two ways:
Assuming you only register this listener with the TextField text property, the ObservableValue passed to the changed(...) method is a reference to this textProperty . It has a getBean() method that returns a TextField . So you can do
StringProperty textProperty = (StringProperty) observable ; TextField textField = (TextField) textProperty.getBean();
This will obviously break (with a ClassCastException ) if you register a listener with something other than textProperty from TextField , but it allows you to reuse the same instance of the listener.
A more reliable way would be to create a listener class as an inner class instead of an anonymous class and keep a reference to the TextField :
private class TextFieldListener implements ChangeListener<String> { private final TextField textField ; TextFieldListener(TextField textField) { this.textField = textField ; } @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
and then
this.firstTextField.textProperty().addListener(new TextFieldListener(this.firstTextField));
and etc.
source share