I want to know how can we perform action when mouse is double clicked in a component.
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);
}
}
});