58

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

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
Suraj Air
  • 2,063
  • 4
  • 22
  • 33

4 Answers4

98
public void mouseClicked(MouseEvent event)
{
  if (event.getClickCount() == 2 && event.getButton() == MouseEvent.BUTTON1) {
    System.out.println("double clicked");
  }
}
Darwin
  • 4,686
  • 2
  • 30
  • 22
kaliatech
  • 17,579
  • 5
  • 72
  • 84
24

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:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
21

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.

Tuna
  • 2,937
  • 4
  • 37
  • 61
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 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. – Benj Jun 05 '15 at 13:00
  • 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. – davidxxx Jun 06 '15 at 18:33
  • 2
    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 Jun 08 '15 at 11:37
  • It wasn't very clear, please excuse me :) But your code is correct if the user is not sneaky ;) – Benj Jun 08 '15 at 11:38
  • @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) – davidxxx Jun 08 '15 at 18:54
  • You're welcome ! See you in another discussion, maybe. – Benj Jun 09 '15 at 12:27
  • 1
    And do you belief this ... such a low quality question ... and still more upvotes than any of mine. hrmpf. – GhostCat Sep 18 '17 at 11:47
  • What about checking `e.getClickCount() % 2 == 0` to detect multiple double clicks within `"awt.multiClickInterval"`? – Marcono1234 Aug 26 '18 at 18:12
1

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);
        }
    }
});
Warren
  • 179
  • 2
  • 7