0

I am trying to create a TextArea in JavaFX which only accepts integer values? Can anyone give me advice as to how to implement this?

onagh
  • 1
  • 1
    See linked question - should work similarly for `TextArea` as does for `TextField`. – Itai Apr 07 '17 at 15:05
  • 1
    Presumably in a `TextArea` you would want to allow newlines at least, and possibly whitespace in general, so you would need to modify the regex in that answer. – James_D Apr 07 '17 at 15:18

1 Answers1

1

Use a TextFormatter:

TextArea textArea = new TextArea();
// allow digits and whitespace:
Pattern allowedText = Pattern.compile("[0-9\\s]*");
TextFormatter formatter = new TextFormatter((TextFormatter.Change c) -> {
    if (allowedText.matcher(c.getText()).matches()) {
        return c ;
    } else {
        return null ;
    }
});
James_D
  • 201,275
  • 16
  • 291
  • 322