1

I have done research into this for a while now and have found very little information on SWT MouseWheelListeners. Looking to see if any of you have encountered this or have a link to something that could help explain them.

I am trying to find out information on the SWT MouseWheelListener and how it is used appropriately. I am attempting to use the MouseWheelListener to create a zoom effect on a composite which draws multiple composite objects on it.

In essence, when wheeling up zoom in by redrawing the canvas at twice normal size, repainting objects on the canvas in a proportional layout, and moving the focus to the point wheeled on.

My questions are the following: Is it possible to use a MouseWheelListener on a Composite or is the listener only for objects like scrolled composites (I know the method is there; nothing is happening when I attempt to scroll on my object (including at debug)? How to kick off a MouseScrolledEvent on a Composite if possible? How to differentiate between wheel up and wheel down (e.count is positive for up & negative for down)?

Code follows:

public TagCloudComposite(Composite parent, int style) {
    super(parent, style);
    addMouseWheelListener(new MouseWheelListener() {
        public void mouseScrolled(MouseEvent e) {
            int count = e.count;
            System.out.println(count);
            // int direction = (Math.abs(count) > 0) ? UP : DOWN;
            // changeBackground(direction);
        }
    });
    this.setLayout(new FillLayout());
    this.setMinWeight(1);
    this.setMaxWeight(100);
    c = new Composite(this, SWT.NONE);
    this.setSize(300, 200);
}
Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54
Adam Green
  • 171
  • 1
  • 7
  • hmmmm still without real solution for me http://stackoverflow.com/questions/7567305/mousewheel-event-doesnt-fire-in-swt-awt-component – mKorbel Oct 24 '11 at 20:52
  • Your component has to have focus for the MouseWheelListener to work. Sometimes, a child component has the focus. – Gilbert Le Blanc May 18 '12 at 19:27

2 Answers2

2

Yes, you're right. "Count" here means number of scroll lines (3 or -3 by default in Windows)

  • It's kind-of a shame that the SWT documentation describes an entirely different purpose of this field (number of times a button has been clicked) and doesn't document this one at all. – Jules Apr 19 '16 at 05:08
2

It is possible to use the MouseWheelListener on a canvas; the only thing is that the event trigger only on the component with the focus.

A canvas don't usually get focus, unless you add a key listener to it (even an empty one).

(I know the question is quite old, but faced with the same issue google directed me there so here's a solution).

Cley Faye
  • 61
  • 3
  • 1
    Yes I discovered this early on. I believe my method for solving this involved using a Composite instead of a canvas and attaching listeners to the top level canvas and the children composites. Then when scrolling taking the appropriate zoom action to redraw the contents. Not incredibly efficient, but it worked. – Adam Green Oct 29 '13 at 13:21
  • Thanks. Those who are wondering how to add the keyListener, here is a code snippet: `public class MouseWheelDemo extends Canvas { addKeyListener(mouseListener); class MapMouseListener implements KeyListener { @Override public void keyPressed(KeyEvent e) { // empty methods works } @Override public void keyReleased(KeyEvent e) { // empty methods works } } ` – Junaed Jun 12 '19 at 13:06