Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javafx HTMLEditor scrollpane scrolls on Space Key

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();
        }
    }
    }
});
like image 431
tobias Avatar asked Jan 09 '13 09:01

tobias


1 Answers

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();
    }
}

});

like image 123
Ian Bibby Avatar answered Oct 20 '22 11:10

Ian Bibby