Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Double Click(Recognizing a double click)
Double Click [message #536018] Wed, 26 May 2010 16:26 Go to next message
Thomas Kratzke is currently offline Thomas KratzkeFriend
Messages: 5
Registered: May 2010
Junior Member
If I add a MouseListener, I seem to be notified of the following events (and in this order):
Down
Up
Down
Double-click
Up.

If I want to ignore the the first up if there is a double coming soon, how would I do that?

Thanks
Re: Double Click [message #536037 is a reply to message #536018] Wed, 26 May 2010 18:13 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Ludger.Heisterkamp.technotrans.de

Hello Thomas,

you have to wait with the Up-Action to see if a DoubleClick-Event occurs.

Something like this:

public static final int MAX_DOUBLECLICK_DELAY = 400;
[...]
widget.addMouseListener(new MouseListener() {

Thread t;
long time=0;

final Runnable r = new Runnable() {
public void run() {
try {
Thread.sleep(MAX_DOUBLECLICK_DELAY + 100);
System.out.println("Up");
} catch (InterruptedException e) {
System.out.println("Up ignored");
}
}
};

public void mouseDoubleClick(MouseEvent arg0) {
if((System.currentTimeMillis()-time)<MAX_DOUBLECLICK_DELAY)
t.interrupt();
System.out.println("Double");
}

public void mouseDown(MouseEvent arg0) {
System.out.println("Down");
}

public void mouseUp(MouseEvent arg0) {
time=System.currentTimeMillis();
t=new Thread(r);
t.start();
}

});

Best regards,
Ludger
Re: Double Click [message #536106 is a reply to message #536037] Thu, 27 May 2010 05:58 Go to previous messageGo to next message
Praveen  is currently offline Praveen Friend
Messages: 86
Registered: July 2009
Member
If the 2nd MouseUp/Down mouse events are to be controlled, then it would
be very easy to track them through event.count field. However, since the
first MouseUp event has to be controlled, then a mechanism similar to
the one posted above should suffice. However, I would suggest minor
changes for the mechanism to act correctly in all scenarios.

> public void mouseDoubleClick(MouseEvent arg0) {
> if((System.currentTimeMillis()-time)<MAX_DOUBLECLICK_DELAY)
> public void mouseUp(MouseEvent arg0) {
> time=System.currentTimeMillis();

The 'time' variable has to be calculated against the event.time field,
which indicates the exact time when the mouse event has happened.
Considering the system time might draw to inaccuracy due to the system
thread scheduling algorithm (when the thread is delayed for the
execution). So, all the time calculations should be dealt with
event.time fields only.
Re: Double Click [message #536269 is a reply to message #536106] Thu, 27 May 2010 14:01 Go to previous message
Thomas Kratzke is currently offline Thomas KratzkeFriend
Messages: 5
Registered: May 2010
Junior Member
Thanks very much to both of you for the help.

It seems to me as if I still need the event.count in the upEvent. I'm trying to process the Up Event if there's no double-click, and just the double-click otherwise. Consider the enclosed code. It's exactly like yours except that I made an abstract class out of it and create it from my main.

I stored the _upEvent so I could give it to my "processSingleClick."

If I don't check for event.count in the moueUp, I get:
processDouble
processSingle.

But anyway, this is a lot cleaner and better than what I had.

Thanks again.




import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
* This class is used to process a click or a double click and to tell the
* difference.
*/
abstract public class ClickListener implements MouseListener {
private final int _maxDoubleClickTime;
private Thread _t;
private int _time = 0;
private final Runnable _r;
private MouseEvent _upEvent;

abstract public void processSingleClick(MouseEvent upEvent);

abstract public void processDoubleClick(MouseEvent doubleClickEvent);

public ClickListener() {
this(Display.getCurrent());
}

public ClickListener(final Display display) {
_maxDoubleClickTime = display.getDoubleClickTime();
_r = new Runnable() {
public void run() {
try {
Thread.sleep(_maxDoubleClickTime + 100);
processSingleClick(_upEvent);
} catch (final InterruptedException e) {
}
}
};
}

public void mouseDoubleClick(final MouseEvent doubleClickEvent) {
if (doubleClickEvent.time - _time < _maxDoubleClickTime) {
_t.interrupt();
processDoubleClick(doubleClickEvent);
}
}

public void mouseDown(final MouseEvent arg0) {
}

public void mouseUp(final MouseEvent upEvent) {
if (upEvent.count == 1) {
_time = upEvent.time;
_upEvent = upEvent;
_t = new Thread(_r);
_t.start();
}
}

public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
final Text helloWorldText = new Text(shell, SWT.NONE);
helloWorldText.setText("Hello World");
helloWorldText.addMouseListener(new ClickListener() {
@Override
public void processDoubleClick(final MouseEvent doubleClickEvent) {
System.out.println("Double" + doubleClickEvent.count);
}

@Override
public void processSingleClick(final MouseEvent upEvent) {
System.out.println("Single" + upEvent.count);
}
});
helloWorldText.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

[Updated on: Thu, 27 May 2010 15:59]

Report message to a moderator

Previous Topic:Toolbar SWT 3.6
Next Topic:MenuBar
Goto Forum:
  


Current Time: Wed Apr 24 20:17:05 GMT 2024

Powered by FUDForum. Page generated in 0.03150 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top