0

I would like to know whether it is possible to make text area in JavaFX to accept only text format, no numbers - in the worst case at least to say to user when typing numbers inside this text area that it's forbidden.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
  • 1
    You should work on the question yourself and add some code. Please verify here: https://stackoverflow.com/help/how-to-ask – rainer Apr 01 '18 at 14:23
  • 1
    [This](https://stackoverflow.com/questions/49571994/hello-i-want-to-create-a-javafx-textfield-where-the-user-can-only-input-in-the/49575480#49575480) is for `TextField`. I am guessing `TextArea` is a similar approach. – SedJ601 Apr 01 '18 at 14:25

2 Answers2

-1

Solution: is quite simple, just check the text inside text area with this method :

 if (!Pattern.matches("[a-zA-Z]+", userInputText.getText())){
            System.out.println("wrong input");
        }

I have personally added this onHandleKeyReleased action event:

@FXML
public void handleKeyReleased() {
    String text = userInputText.getText();
    boolean disableButtons = text.isEmpty() || text.trim().isEmpty() || (!Pattern.matches("[a-zA-Z]+", userInputText.getText())) ;
    okButton.setDisable(disableButtons);
}

So after user writes any number inside the text field, the button for confirming his action will be disabled, so it will look like this:

correct input

incorrect input

This should work with input UI element within JavaFx.

  • no ... problems: a) doesn't catch incorrect input in c&p b) that pattern isn't working in other Locales (rejects more chars than allowed) To fix the former use a TextFormatter, to fix the latter use a Format – kleopatra Apr 02 '18 at 09:37
-2

for number : What is the recommended way to make a numeric TextField in JavaFX?

try invert it (near match !)

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]", ""));
        }
    }
});