3
I have a Textfield in javafx, but I can’t find the Maxlength property. In C# and QT5, I have already made some applications, and it is very simple to find this property.
I have found some answers, however I am in doubt if I really need to do this "at hand" as the answers say.
There is this property in Textfield in Javafx or not ?
Response example for maxlength in Javafx:
public static void addTextLimiter(final TextField tf, final int maxLength) {
tf.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(final ObservableValue<? extends String> ov, final String oldValue, final String newValue) {
if (tf.getText().length() > maxLength) {
String s = tf.getText().substring(0, maxLength);
tf.setText(s);
}
}
});
}
That’s right, you don’t have a property for that purpose and you’ll need to implement your limiter. What you did is right, but it can also create a class that you inherit from
TextField
and override the methodsreplaceText
andreplaceSelection
, making the same validation that makes in this Listener of your code.– Renan Gomes
@Renan why don’t you put your answer as a de facto answer? Your answer is correct.
– Fagner Fonseca