[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: Confusing mouseDown / mouseUp / mouseDoubleClick

Enric Tordera wrote:
I've developed a custom widget. It has got a Composite, where I want to catch some mouse events. So I used "addMouseListener" and defined a "MouseListener" that implements "mouseDown", "mouseUp" and "mouseDoubleClick".

I just want to detect single clicks and double clicks on the widget. Single clicks mean different things than double clicks in my application. I wrote my code under "mouseUp" for single clicks and under "mouseDoubleClick" for double clicks.

The problem is that when the user double clicks on the widget, the code at mouseDoubleClick is called, but also the code at mouseUp is called twice.

Which is the best way to detect the double click and avoid the code at "mouseUp" or "mouseDown" being executed in that case?

Thanks in advance

- Enric

Use Display.timerExec to delay the single-click event handler until you're sure it's not a double-click. I've used this technique to delay updating of viewer filters until the user stops typing in a filter field.


class CancelableRunnable implements Runnable {
  public void cancel() { ... }

  public boolean isCancelled() { ... }

  public void run() {
    if (!isCancelled()) {
      // single click event handler
    }
  }
}

When you receive a mouse-up event, create a new CancelableRunnable instance, assign it to a field where the double-click listener has access to it, and then call:

display.timerExec(display.getDoubleClickTime(), runnable);

In the double-click event handler, check to see if the cancelable runnable is non-null, and if so, cancel it.

Let me know how it works.

Matthew