I made a textfield in JavaFX of username, I want to run a method that checks if there is a space or not! , is there a way to do that ?
Asked
Active
Viewed 1,239 times
0
-
1Try using a [`TextFormatter`](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextFormatter.html) and post a specific question if you can't get it to work. – James_D Feb 01 '17 at 01:46
-
Possible duplicate of [What is the recommended way to make a numeric TextField in JavaFX?](http://stackoverflow.com/questions/7555564/what-is-the-recommended-way-to-make-a-numeric-textfield-in-javafx) – MikaelF Feb 01 '17 at 06:07
1 Answers
3
TextFormatter's filter
This filter(UnaryOperator) allows a user to intercept and modify any change done to the text content. Here is an example which sets no change on space value.
TextField field = new TextField();
field.setTextFormatter(new TextFormatter<>(change -> {
if (change.getText().equals(" ")) {
change.setText("");
}
return change;
}));

Shekhar Rai
- 2,008
- 2
- 22
- 25