Javafx HTMLEditor scrollpane scrolls space Key

I have a VBox inside a ScrollPane that contains an HTMLEditor and other things.

When I type text inside HTMLEditor every time I press the spacebar, I get a space inside the editor, as expected, but it also scrolls down Scrollpane. At first I worked on this by adding an EventFilter to the Scrollpane and consuming the KEY_PRESSED event. But now I need this event inside HTMLEditor.

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

You can reproduce this also with javafx Scene Builder:

Scrollpane-> VBox (larger than Scrollpane, so scrollbars will appear) -> 2 * HTMLEditor, Preview in Window, press the spacebar.


Solved: Added EventFilter in HTMLEditor, which consumes 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(); } } } }); 
+4
source share
2 answers

I ran into a similar problem. I did this to pass the filtered event to my event handler method just before it was consumed. For your case, it would look something like this (suppose you have a KeyEvent handler method that you called 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(); } } 

});

+2
source

Create your own widget that extends HTMLEditor and adds a listener for the clicked event.

 setOnKeyPressed(event -> { if (event.getCode() == KeyCode.SPACE || event.getCode() == KeyCode.TAB ) { // Consume Event before Bubbling Phase, -> otherwise Scrollpane scrolls event.consume(); } }); 
0
source

All Articles