how to distinct single mouseclicks from double ones
MouseEvent.getClickCount() starts always with returning a 1.
I want to react differently on single or double clicks.
clearly I have to wait, to see what nr of clicks I get
my question is : does anyone know an elegant way to do this?
preferably in mousePressed()?
thanks,
peter
Try with one of these, ....
Option1
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
System.out.println( " and it's a double click!");
}
else{
System.out.println( " and it's a simple click!");
}
}
//-//
Option2
public class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 3) {
// triple-click
} else if (evt.getClickCount() == 2) {
// double-click
}
}
}
http://javaalmanac.com/egs/java.awt.event/MultiClicks.html
Message was edited by:
jugp
jugpa at 2007-7-14 22:09:46 >

thanks for your response, but my problem is (seems to me) somewhat more complicated...
the returned click-count is clear, but my program puts a "point" somewhere (evt.getPoint()) on my Graphics-screen in reaction on 1 click.
this is my "normal" reaction, however, when I (later) detect double-clicking (meaning in my case that I want to select something (eg. a Shape) on the screen, I have to "undo" the setting from the "point" I just set.
Since all my screen-info is stored in a Vector this is a bit dumb.
It is not really a big problem, but my question remains the same :
how do I ignore click nr 1, when it turns out that a second one follows?
> Try with one of these, ....
>
> Option1
>
> public void mouseClicked(MouseEvent e) {
>if (e.getClickCount() == 2) {
>System.out.println( " and it's a double click!");
>}
>else{
> System.out.println( " and it's a simple
> click!");
>}
>
>
>
>
> //-//
> [b]
> Option2[/b]
>
> [code]public class MyMouseListener extends
> MouseAdapter {
>public void mouseClicked(MouseEvent evt) {
>if (evt.getClickCount() == 3) {
>// triple-click
> } else if (evt.getClickCount() == 2) {
>// double-click
> }
> ode]
>
>
> http://javaalmanac.com/egs/java.awt.event/MultiClicks.
> html
>
> Message was edited by:
> jugp
Probably the only way would be to schedule a timer at the first click. When a second click would occur, you cancel the timer and respond to the double-click.
Now the only question is, how long the timer should wait. Unfortunately you cannot retrieve the double-click speed from the OS, which would make your application inconsistent with the OS.
For usability i would recommend against it it. Since the usual double-click idiom is that the first click selects and the second(double-click) performs an action on the selection.
Tiom_a at 2007-7-14 22:09:46 >
