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?
1 Answer
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 ; } });
TextAreaas does forTextField.TextAreayou would want to allow newlines at least, and possibly whitespace in general, so you would need to modify the regex in that answer.