Is there an event that captures every change that occurs in a JAvaFX TextField?
Is there an event that captures every change that occurs in a JAvaFX TextField?
A bit generic question because there are several properties of TextField that can be observed by the programmer. Looking at the documentation we have:
TextInputControl inherited properties (These are the main ones):
anchor , caretPosition , editable , font , length , promptText , redoable , selectedText , selection , textFormatter , text , undoable
All of these properties have methods with the word Property at the end (eg, textProperty()
). In addition to the aforementioned methods we have 104 properties that can be observed.
I'm going to talk about the properties that are usually handled, what they do and how to capture the changes.
textProperty () : I think it's the most common. This property wraps the inserted / deleted text in the component.
textfield.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String oldValue, String newValue) {
// oldValue = Texto anterior a edição
// newValue = Texto atual
System.out.println(newValue);
}
});
// Código acima usando lambdas
text.textProperty().addListener((ObservableValue<? extends String> ov, String oldValue, String newValue) -> {
System.out.println(newValue);
});
Note: We typically use ChangeListener (as the name indicates it captures changes) but there is the option to use a InvalidationListener to notify entry in an invalid state.
lengthProperty () : This property involves the size of the component text as the user types / deletes the content.
textfield.lengthProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> ov, Number oldValue, Number newValue) {
if(newValue.intValue() > 10)
System.out.println("Texto muito longo");
}
});
// Código acima usando lambdas
textfield.lengthProperty().addListener((ObservableValue<? extends Number> ov, Number oldValue, Number newValue) -> {
if(newValue.intValue() > 10)
System.out.println("Texto muito longo");
});
As you may have already seen, the code for capturing changes is similar between properties. However, TextField has a particularity in this area which is the TextFormatter. With it you have the possibility to analyze a change before it is implemented (Only for Java 8u40 +)
textfield.setTextFormatter(new TextFormatter<>(change ->
{
if (change.getControlNewText().isEmpty()) {
return change;
}
String text = change.getControlNewText();
for (int i = 0; i < text.length(); i++){
if(!Character.isDigit(text.charAt(i))){
return null;
}
}
return change;
}));
With the getControlText () and getControlNewText () methods we get the current and post-editing text (without the change being made yet). The above code aborts (return null) any changes that contain a nonnumeric element in your text. I will not go into detail on how to use TextFormatter because it is very broad, but you can see examples here .
Add a Listener to the textProperty of your TextField:
textField.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("Valor antigo = " + oldValue + ". Valor novo = " + newValue);
});