javaswtscrolledcomposite

How to remove a mouse wheel listener in SWT


I am trying to remove and replace a mouse wheel listener on a ScrolledComposite in SWT. The scrolled composite has a removeMouseWheelListener method, however it has no way of gaining access to a the mouse wheel listener to remove it. I have tried the getListeners() method:

MouseWheelListener mouseWheelListener = (MouseWheelListener) scrollable.getListeners(SWT.MouseWheel)[0];

but this produces an a casting error so getListeners must not retrieve the same type of listeners. I have tried creating a new listener and removing it from the ScrolledComposite:

MouseWheelListener scroller = new MouseWheelListener() {
    @Override
    public void mouseScrolled(MouseEvent e) {
        Point currentScroll = scrollable.getOrigin();
        scrollable.setOrigin(currentScroll.x, currentScroll.y - (e.count * 5));
    }
};
scrollable.removeMouseWheelListener(scroller);

This does not remove the listener though. Of course, if I had access to the original MouseWheelListener that was added this would not be a problem, but I don't. Thank you.


Solution

  • getListeners will return a listener of type TypedListener for a mouse wheel listener.

    TypedListener.getEventListener() will return the MouseWheelListener.