How can I generate a new event to handle whenever TextField's text is changed?
Asked
Active
Viewed 3.2k times
3 Answers
27
Or Use ChangeListener
interface.
textField.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
System.out.println(" Text Changed to " + newValue + ")\n");
}
});

Fevly Pallar
- 3,059
- 2
- 15
- 19
-
This is useful! But any idea how I can register the change say when I hit the 'Enter' key? – alchemist.gamma Oct 17 '17 at 02:02
-
Well then put this listener inside the listener for the *Enter*, inside the *Enter* listener's callback to be precise. – Fevly Pallar Oct 18 '17 at 05:28
-
4If you want to catch the Enter key, I believe you want `setOnAction`; my understanding is that by default, pressing Enter in a text field "submits" the form by invoking the registered action, if any. – Tim has moved to Codidact Aug 18 '19 at 23:12
20
Register a listener with the TextField
s textProperty
:
textField.textProperty().addListener((obs, oldText, newText) -> {
System.out.println("Text changed from "+oldText+" to "+newText);
// ...
});

James_D
- 201,275
- 16
- 291
- 322
2
my solution:
....
String temp;
....
//previously in some method when starting to take initial value of the textfield and save it in a temporal
temp=id_textfield.getText();
....
//when losing focus we check if the new value is different
id_textfield.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
//focus in
if (newValue) {
temp = id_textfield.getText();
}
//focus out
if (oldValue) {
if (!(temp.equals(id_textfield.getText()))) {
System.out.println("New String")
}
}
}
});

agriarte
- 75
- 8