I am working with a JavaFX 2.2 project and I have a problem using the TextField control. I want to limit the characters that users will enter to each TextField but I can\'t
I'm using a simpler way to both limit the number of characters and force numeric input:
public TextField data;
public static final int maxLength = 5;
data.textProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends String> observable,
String oldValue, String newValue) {
try {
// force numeric value by resetting to old value if exception is thrown
Integer.parseInt(newValue);
// force correct length by resetting to old value if longer than maxLength
if(newValue.length() > maxLength)
data.setText(oldValue);
} catch (Exception e) {
data.setText(oldValue);
}
}
});