I want to control the input into a Javafx TextField so that I can allow only Numeric input, and so that if the max characters are exceeded, then no change will be made to th
Final solution. Disallows alphabetic and special characters and enforces character limit.
import javafx.scene.control.TextField;
public class AttributeTextField extends TextField{
public AttributeTextField() {
setMinWidth(25);
setMaxWidth(25);
}
public void replaceText(int start, int end, String text) {
String oldValue = getText();
if (!text.matches("[A-Za-z]") && !text.matches("[\\\\!\"#$%&()*+,./:;<=>?@\\[\\]^_{|}~]+")) {
super.replaceText(start, end, text);
}
if (getText().length() > 2 ) {
setText(oldValue);
}
}
public void replaceSelection(String text) {
String oldValue = getText();
if (!text.matches("[A-Za-z]") && !text.matches("[\\\\!\"#$%&()*+,./:;<=>?@\\[\\]^_{|}~]+")) {
super.replaceSelection(text);
}
if (getText().length() > 2 ) {
setText(oldValue);
}
}
}