JavaFX textarea listeners

1k views Asked by At

Is it possible to turn off arrow key listeners in TextArea when it has focus. I am reffering mostly to moving scrollpane with them when there is more text than space in area. I'm trying to avoid this.

Thanks for suggestion.

1

There are 1 answers

2
beatngu13 On

You could use an event filter. Filters receive events during the event capturing phase (1st, down the scene graph) of event processing, whereas handlers are triggered during the event bubbling phase (2nd, up the scene graph). Have a look at this snippet:

TextArea textArea = new TextArea();
textArea.addEventFilter(KeyEvent.ANY, event -> {
    KeyCode code = event.getCode();
    boolean isArrowKey = code == KeyCode.UP || code == KeyCode.RIGHT
            || code == KeyCode.DOWN || code == KeyCode.LEFT;
    if (isArrowKey) {
        event.consume();
    }
});

The consume() method stops further propagation of the event. As a result, no (key event) listener will be triggered. But this also disables the ability to navigate through the arrow keys—is that really what you want?

EDIT: in regards to your comment, this answer states:

The behavior for controlling the scroll bars is internally coded in the TextAreaSkin (which in Java 8 is not part of the public JavaFX API). You could copy or subclass the TextAreaSkin to customize its behavior and then attach your customized skin to your node. This is really the "proper" way to customize internal control behavior in the way in which you wish.