JavaFX: how to create a scrollpane pan only on the middle key?

When panning is allowed, the scrollpane pane of JavaFX displays all mouse events:

scrollPane.setPannable(true);

How can I restrict scrollpane to pan only over mouse events while still allowing all events to reach the contents of stackpane?

Solution

You should use all events except the middle button event in the content event handler:

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

This is effective because scrollpane also pans through the event handler and invokes the event handler from bottom to top Therefore, if we use events through child nodes, it will not be able to access the scrollpane viewrect for translation

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>