Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: How to make a ScrollPane pan only on middle button?

JavaFX's ScrollPane panes on all mouse events when allowed to pan:

scrollPane.setPannable(true);  

How to limit the ScrollPane to pan only on middle mouse events, while still allowing all events to get to the StackPane's content?

like image 506
NightRa Avatar asked Feb 08 '23 19:02

NightRa


1 Answers

You should consume all events except the middle button events inside the content's event handler:

// Let the ScrollPane.viewRect only pan on middle button.
imageLayer.addEventHandler(MouseEvent.ANY, event -> {
    if(event.getButton() != MouseButton.MIDDLE) event.consume();
});

This works because the ScrollPane pans via an event handler too, and event handlers are invoked bottom-up. Thus if we consume the event via the child, it won't get to the ScrollPane viewRect which does the panning.

like image 199
NightRa Avatar answered Feb 15 '23 11:02

NightRa