Zooming in JavaFx: ScrollEvent is consumed when content size exceeds ScrollPane viewport

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 13:40:30
Skjalg

OK, so I finally found a solution to my problem.

By merely substituting the line

   scrollPane.setOnScroll(new ZoomHandler(innerGroup));

with

    scrollPane.addEventFilter(ScrollEvent.ANY, new ZoomHandler(innerGroup));

it now works as expected. No mystic Rectangle or other hacks are needed.

So the next question is why? According to this excellent article on Processing Events,

An event filter is executed during the event capturing phase.

while

An event handler is executed during the event bubbling phase.

I assume this is what makes the difference.

The following workaround seems to be giving better results:

  1. Set onscroll event on the outer group to steal the event from the scroll pane.
  2. Add a opaque rectangle which covers the whole screen, so that you don't miss the scroll event. Apparently you can miss scroll event if you don't hit a shape.

    Rectangle opaque = new Rectangle(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    opaque.setOpacity( 0 );
    outerGroup.getChildren().add( opaque );
    outerGroup.setOnScroll(new ZoomHandler(innerGroup));
    

In my case I have updated the following line
if (scrollEvent.isControlDown()) {
with
if (!scrollEvent.isConsumed()) {.... along with the change you have posted.... :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!