javafx HTMLEditor scrollpane scrolls on Space Key
Asked Answered
D

2

6

I have a VBox inside a ScrollPane wich contains a HTMLEditor and other stuff.

When I type text inside the HTMLEditor each time I hit the Space Bar, I get a whitespace inside the editor as expected but also the Scrollpane scrolls down. First I worked around this by adding a EventFilter to the Scrollpane and consume the KEY_PRESSED event. But now I need this event inside the HTMLEditor.

So my question: is there any Flag to tell the Scrollpane not to scroll on KeyCode.SPACE or is there a way to route the input Focus/ Key Events only to the HTMLEditor, bypassing the Scrollpane? Or a way to filter this event only on the Scrollpane?

You can reproduce this also with javafx Scene Builder:

Scrollpane->VBox(larger than Scrollpane so Scrollbars appear)->2*HTMLEditor, Preview in Window, hit the Space Bar.


Solved: Added an EventFilter to the HTMLEditor, which consumes the KeyCode.SPACE on KEY_PRESSED.

htmlEditor.addEventFilter( KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
    @Override
public void handle(KeyEvent event) {

    if (event.getEventType() == KeyEvent.KEY_PRESSED){
        // Consume Event before Bubbling Phase, -> otherwise Scrollpane scrolls
        if ( event.getCode() == KeyCode.SPACE ){ 
            event.consume();
        }
    }
    }
});
Dannadannel answered 9/1, 2013 at 9:40 Comment(0)
J
3

I just ran into a similar problem. What I did was to pass the filtered event on to my event handler method directly before consuming it. For your case, it would look something like this (assume you have an KeyEvent handler method that you've named onKeyPressed()):

htmlEditor.setOnKeyPressed(new EventHandler<KeyEvent>() {@Override public void handle(KeyEvent t) { onKeyPressed(t); }});

scrollPane.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
    if(t.getCode() == KeyCode.SPACE) {
        onKeyPressed(t);
        t.consume();
    }
}

});

Jammie answered 9/1, 2013 at 16:59 Comment(2)
Thanx for your help, but i have 2 problems with this: 1. the content of the ScrollPane is added, changed and removed at runtime. So in this case i need something like a "KeyPressedBus" on which i would register/unregister the htmlKeyPressed listeners when they are added,changed,removed to the Scrollpane, and then forward the event to the "KeyPressedBus" which then calls the "onKeyPressed" on the listeners like you suggested.Dannadannel
2. The content of the Scrollpane can have more than 1 HTMLEditor so i also have to check in which editor the User is working.Pretty much overhead for that tiny problem. I assume that the problem has to do something with the InputFocus, 'cause if you replace the HTMLEditor with a TextArea, theres no problem with this. The border of the TextArea is colored like its indicating hey i got the inputFocus. The HTMLEditor has no such coloring.Dannadannel
S
0

Create your own widget that extends the HTMLEditor and add a listener for the pressed event.

setOnKeyPressed(event -> {
    if (event.getCode() == KeyCode.SPACE  
            || event.getCode() == KeyCode.TAB ) {
        // Consume Event before Bubbling Phase, -> otherwise Scrollpane scrolls
        event.consume();
    }
});
Selfseeking answered 2/2, 2017 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.