Reputation: 1
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.
Upvotes: 0
Views: 258
Reputation: 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:
This should work with input UI element within JavaFx.
Upvotes: -1
Reputation: 3079
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]", ""));
}
}
});
Upvotes: -2