identifying double click in java

JavaSwingMouseeventDouble Click

Java Problem Overview


I want to know how can we perform action when mouse is double clicked in a component.

Java Solutions


Solution 1 - Java

public void mouseClicked(MouseEvent event)
{
  if (event.getClickCount() == 2 && event.getButton() == MouseEvent.BUTTON1) {
    System.out.println("double clicked");
  }
}

Solution 2 - Java

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:

Solution 3 - Java

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.

Solution 4 - Java

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);
        }
    }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSuraj AirView Question on Stackoverflow
Solution 1 - JavakaliatechView Answer on Stackoverflow
Solution 2 - JavaSean Patrick FloydView Answer on Stackoverflow
Solution 3 - JavadavidxxxView Answer on Stackoverflow
Solution 4 - JavaWarrenView Answer on Stackoverflow