1

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.

Terrik
  • 17
  • 5
  • 1
    Why would you want to add and remove a listener? I don't understand. If you want a specific mouse wheel listener just add a new one by using addMouseWheelListener() method. But if you don't want your mouse wheel listener to do nothing just don't do anything in public void mouseScrolled( MouseEvent e ) – SomeDude Dec 04 '15 at 20:53
  • There is a MouseWheelListener that gets added to my ScrolledComposite. What I want to be able to do is remove that one, and put a new one in. If it is impossible to do this, why is there a removeMouseWheelListener() method? – Terrik Dec 04 '15 at 21:05

1 Answers1

1

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

TypedListener.getEventListener() will return the MouseWheelListener.

greg-449
  • 109,219
  • 232
  • 102
  • 145