I need to perform validation on several TextFields when text is changed. The validation is exactly the same, so I thought I use a single procedure. I can't use onInputMethodTextChanged because I need to perform validation even when control does not have focus. So I added a ChangeListener to textProperty.
private TextField firstTextField;
private TextField secondTextField;
private TextField thirdTextField;
protected void initialize() {
ChangeListener<String> textListener = new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
// Do validation
}
};
this.firstTextField.textProperty().addListener(textListener);
this.secondTextField.textProperty().addListener(textListener);
this.thirdTextField.textProperty().addListener(textListener);
}
However, while performing validation, there is no way to know which TextField triggered the change. How can I obtain this information?
ClassCastException
, I think you can expectControl
rather thanTextField
and check if theControl
is aTextField
, something likeif(mControl instanceof TextField) ((TextField) mControl).doSomething()
– Crumby