I want to make sure that the user can only write Numbers in the text field.
But whenever I call textfield.setText(oldValue);
, it won't update the text in the text field (it's not deleting the invalid characters).
public class Controller{
@FXML
private TextField textfield;
@FXML
void initialize(){
System.out.println("initialized setup window");
textfield.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue,
String newValue) {
if (!newValue.matches("\\d*")) {
System.out.println("wrong character!");
System.out.println("new Value : "+newValue);
System.out.println("old Value : "+oldValue);
textfield.setText(oldValue);
}
}
});
}