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
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:
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.... :)