Question

Is there way to implement mouse wheel listener for ScrollPane? I looked at couple of options:

  • Viewport does not seem to have mouse wheel listener
  • Played with gef's MouseWheelHelper but the y value only changes when I click within the ScrollPane, not when mouse wheel is scrolled. Although event is being fired.

Also, there is no vertical scroll bar for the ScrollPane by design.

Was it helpful?

Solution

Ok, the magic number here is org.eclipse.swt.widgets.Event.count. This number tells you "number of lines or pages to scroll using the mouse wheel". Combining this and MouseWheelHelper interface I was able to make the scroll with mouse wheel work:

public class MyEditPart extends TreeEditPart implements MouseWheelHelper{
private static final int SCROLL_OFFSET = 10;
...
    ...
@Override
public void handleMouseWheelScrolled(Event event) {
    pane.scrollVerticalTo(pane.getViewport().getVerticalRangeModel().getValue() + (event.count * SCROLL_OFFSET));
}

}

What's neat is that by changing SCROLL_OFFSET I can control the speed with which Viewport scrolls.

The only thing to check is definition of Event.count that says:

depending on the event type, the number of following paint events that are pending which may always be zero on some platforms, or the number of lines or pages to scroll using the mouse wheel, or the number of times the mouse has been clicked

Not sure how this will play out on other OSes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top