Determine calling node inside JavaFX change listener

There are two ways:

Assuming you only register this listener with the text property of a TextField, the ObservableValue passed into the changed(...) method is a reference to that textProperty. It has a getBean() method which will return the TextField. So you can do

StringProperty textProperty = (StringProperty) observable ;
TextField textField = (TextField) textProperty.getBean();

This will obviously break (with a ClassCastException) if you register the listener with something other than the textProperty of a TextField, but it allows you to reuse the same listener instance.

A more robust way might be to create the listener class as an inner class instead of an anonymous class and hold 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) {
    // do validation on textField
  }
}

and then

this.firstTextField.textProperty().addListener(new TextFieldListener(this.firstTextField));

etc.

Tags:

Java

Javafx 2