identifying double click in java
70,112
Solution 1
public void mouseClicked(MouseEvent event)
{
if (event.getClickCount() == 2 && event.getButton() == MouseEvent.BUTTON1) {
System.out.println("double clicked");
}
}
Solution 2
Assuming you mean in Swing, assign a MouseListener to your Component:
addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2){
// your code here
}
}
});
Reference:
- Java Tutorial: How to write a Mouse Listener
Solution 3
The e.getClickCount()==2
is not enough if you want to allow your users to do multiple double clicks in a short delay.
You are limited by the desktop configuration.
You can get it by looking the result of Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
A good way to bypass the problem is not to use the getClickCount()
check but to use a Timer
where you can choose the interval max between your clicks and to handle by oneself the count (very simple).
The code associated :
boolean isAlreadyOneClick;
@Override
public void mouseClicked(MouseEvent mouseEvent) {
if (isAlreadyOneClick) {
System.out.println("double click");
isAlreadyOneClick = false;
} else {
isAlreadyOneClick = true;
Timer t = new Timer("doubleclickTimer", false);
t.schedule(new TimerTask() {
@Override
public void run() {
isAlreadyOneClick = false;
}
}, 500);
}
}
Tested with Win Xp OS and perfect.
Author by
Suraj Air
Updated on July 09, 2022Comments
-
Suraj Air almost 2 years
I want to know how can we perform action when mouse is double clicked in a component.
-
Benj about 9 yearsIntelligent use of a
Timer
to unset a flag, more complex than a comparison between two instants but easier to use. The drawback I see is : where to store theisAlreadyOneClick
? Seems to bring problems while solving one another. This solution will also have "holes" in its behavior if (let's imagine) the clicks are done very quickly : each time theTimer
will reset the flag and only at this time, there will be a true double click handled. Also, I think this method isn't really GC and resources friendly. -
davidxxx about 9 yearsHello Benj I don't manage to understand the relation between the Garbage Collector and the flag. Do you think that the way to proceed can break something ? Please, could you precise further your example and its consequences ? Sorry, I don't see what you try to show.
-
Benj about 9 yearsThere are two things I see : 1/ It's just that each time the "else" part will be run into, a new
Timer
object will be created. I would move this timer as a field of the object to allow managing it from other places such as right click or somewhat. 2/ These timers will be unflagging your double click each 500ms, then if you have to double click twice, there are chances for the last not to work as expected. -
Benj about 9 yearsIt wasn't very clear, please excuse me :) But your code is correct if the user is not sneaky ;)
-
davidxxx about 9 years@Benj, For the first remark, you are right. In practice, if relevant, it would be a good idea to use a single instance of the timer. And in a general way, use a single instance would be a good idea. We will not gain a lot of in performance but I agree because it's better to factorize the code when possible. For my part, I have not had this need in the part of my application. The fast double click handling was needed in a single use case. For the second remark, you can use any delay (500 or more like 1000 ms ). Anyway, thank you for this exchange (especially the first remark)
-
Benj about 9 yearsYou're welcome ! See you in another discussion, maybe.
-
Sean Van Gorder almost 7 yearsYou'll probably want to check
event.getButton() == MouseEvent.BUTTON1
as well, to only count double-clicks with the left mouse button. -
GhostCat over 6 yearsAnd do you belief this ... such a low quality question ... and still more upvotes than any of mine. hrmpf.
-
Marcono1234 almost 6 yearsWhat about checking
e.getClickCount() % 2 == 0
to detect multiple double clicks within"awt.multiClickInterval"
? -
Marcono1234 almost 6 years@SeanVanGorder or
SwingUtilities.isLeftMouseButton(event)