-1

I used this code to make it numeric:

textField.textProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
        if (!newValue.matches("\\d*"))
            textField.setText(newValue.replaceAll("[^\\d]", ""));
    }
});

, but I also want it to accept 1-digit integers only.

0009laH
  • 1,735
  • 11
  • 24
  • 4
    you want to use a textformatter – kleopatra May 10 '22 at 06:55
  • You mention wanting to only accept single-digit integers, but your code uses `\d*` for the regex, which obviously will match any number of digits (including zero digits). If you want exactly one digit, then just use `\d`. If you want to match zero or one digit, then use `\d{0,1}`. Beyond that, as the first comment and duplicate mention, you want to use a `TextFormatter` to implement this behavior. – Slaw May 10 '22 at 22:29

0 Answers0