KeyEvent.consume
does not work in JavaFX.
I’m new to JavaFX (coming from using Swing) and I’m trying to make a program that blocks certain characters from being typed into a TextField
. Now in Swing I could use a KeyListener
and use event.consume();
to prevent the event from being processed and typed into the JTextField
. But in the JavaFX’s EventHandler
while KeyEvent
does have a consume
method it does not work and the character is still typed into the TextField
. Here is the code for the TextField
EventHandler
.
input1TextField.setOnKeyTyped(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent event) {
System.out.println(event.getCharacter());
event.consume();//this does not work
}
});