I want to know how can we perform action when mouse is double clicked in a component.
public void mouseClicked(MouseEvent event)
{
if (event.getClickCount() == 2 && event.getButton() == MouseEvent.BUTTON1) {
System.out.println("double clicked");
}
}
SwingUtilities.isLeftMouseButton(event)
–
Frances 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
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.
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 the isAlreadyOneClick
? 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 the Timer
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. –
Debug 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. –
Debug e.getClickCount() % 2 == 0
to detect multiple double clicks within "awt.multiClickInterval"
? –
Frances My problem is that I have to respond one way if the user single clicks, another if they click more than one time (my Swing VM seems to be able to count up to four clicks when I click multiple times). When I ran the example above, it seemed to count a triple click as a single one. So, here is my rewrite. Basically, I just have a scheduled task that waits until the dust clears and then checks the number of clicks registered. The 400 ms wait seems to work best for me.
JButton jButton = new JButton("Click Me!");
jButton.addMouseListener(new MouseAdapter() {
private int eventCnt = 0;
java.util.Timer timer = new java.util.Timer("doubleClickTimer", false);
@Override
public void mouseClicked(final MouseEvent e) {
eventCnt = e.getClickCount();
if ( e.getClickCount() == 1 ) {
timer.schedule(new TimerTask() {
@Override
public void run() {
if ( eventCnt == 1 ) {
System.err.println( "You did a single click.");
} else if ( eventCnt > 1 ) {
System.err.println("you clicked " + eventCnt + " times.");
}
eventCnt = 0;
}
}, 400);
}
}
});
© 2022 - 2024 — McMap. All rights reserved.
event.getButton() == MouseEvent.BUTTON1
as well, to only count double-clicks with the left mouse button. – Fragment