I want to use a single ChangeListener for multiple TextFields in my application.
I know that you can create a listener object and add this to all TextFields, then you can check via the event key which TextField-Object triggered the event.
With lamda expression I could use this for every TextField, unfortunately this isn't suited for my problem. I use the input of all TextFields so one single Listener should be enough.
TextField textField = new TextField();
textField.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
As Slaw pointed out in his comment, you can easily create a single
ChangeListenerand then apply it to each of yourTextFieldnodes.Here is a complete sample you can run to see it in action:
Now, the above example does not indicate which
TextFieldwas changed. In order to track that, you could create your own inner class that implementsChangeListener<String>and pass yourTextField(or any other data to it):Then, just add a new instance of that
MyChangeListenerto eachTextFieldinstead: