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

前端 未结 3 2061
抹茶落季
抹茶落季 2020-12-31 14:18

I have an application that requires zoom inside a ScrollPane, but with my current approach I\'m still facing 2 challenges. In order to replicate the problem, I have written

相关标签:
3条回答
  • 2020-12-31 14:31

    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.

    0 讨论(0)
  • 2020-12-31 14:45

    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));
      
    0 讨论(0)
  • 2020-12-31 14:48

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

    0 讨论(0)
提交回复
热议问题