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:

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.

Share:
70,112
Suraj Air
Author by

Suraj Air

Updated on July 09, 2022

Comments

  • Suraj Air
    Suraj Air almost 2 years

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

  • Benj
    Benj about 9 years
    Intelligent 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 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.
  • davidxxx
    davidxxx about 9 years
    Hello 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
    Benj about 9 years
    There 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
    Benj about 9 years
    It wasn't very clear, please excuse me :) But your code is correct if the user is not sneaky ;)
  • davidxxx
    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
    Benj about 9 years
    You're welcome ! See you in another discussion, maybe.
  • Sean Van Gorder
    Sean Van Gorder almost 7 years
    You'll probably want to check event.getButton() == MouseEvent.BUTTON1 as well, to only count double-clicks with the left mouse button.
  • GhostCat
    GhostCat over 6 years
    And do you belief this ... such a low quality question ... and still more upvotes than any of mine. hrmpf.
  • Marcono1234
    Marcono1234 almost 6 years
    What about checking e.getClickCount() % 2 == 0 to detect multiple double clicks within "awt.multiClickInterval"?
  • Marcono1234
    Marcono1234 almost 6 years
    @SeanVanGorder or SwingUtilities.isLeftMouseButton(event)