File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes SWT: how do differ between single and double click? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "SWT: how do differ between single and double click?" Watch "SWT: how do differ between single and double click?" New topic

SWT: how do differ between single and double click?

Serge Glaeser

Joined: Jul 27, 2010
Posts: 7
i have a problem with the mouse selection in a tree. i can add a single and double click listener to the three with no problem. the problem i have is that when i double click an item it runs through both listener. Through the mouse down listener twice (first event with one click, second event with count = 2) and then again through the double click listener. However I want to execute different actions depending on the clicks, but since there seems to be always a single click involved this makes it kinda impossible. Is there a way to differ between the clicks, so that the mouse down (single click) listener only reacts when the user really clicked just once?

Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
And how would you know the first click from a 'series of clicks'? You cannot. You simply have to wait if further clicks happen. When there's only one click in a span of time it's a single click. When there're more than one click in that time span, it's a not-single-click. Are you sure you need both actions (click and double-click) in the way you described?

Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10815

I have never worked on SWT, but in Swing you got a method called MouseEvent#getClickCount() which is used to identify double clicks. You might want to check for something similar in SWT.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
David A. Smith

Joined: Sep 08, 2010
Posts: 9

Like mentioned, you have to handle single *and* double clicks. In most applications, double-clicking on something is an extension of single-clicking on that same thing, so it usually ends up okay. Here's some code:

Then of course you need to install the handler...

Rob Spoor

Joined: Oct 27, 2005
Posts: 20269

Hello Dave,

thanks for the input, but I think you missed the "SWT" part. In Swing/AWT this is indeed the way to go.

How To Ask Questions How To Answer Questions
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

I encoutered similar issue last time. Although, MouseEvent has count property, the SWT.MouseDown event is always called first before the SWT.MouseDoubleClick. So the workaround is to wait till the SWT.MouseDoubleClick has been fired, then check the flag which event was fired. The code is something like below.

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46326
Welcome to the Ranch Mason Wardle

You cannot delete posts, but I can.
I agree. Here's the link:
subject: SWT: how do differ between single and double click?
It's not a secret anymore!