identifying double click in java
Asked Answered
R

4

59

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

Riverside answered 29/10, 2010 at 12:6 Comment(0)
C
100
public void mouseClicked(MouseEvent event)
{
  if (event.getClickCount() == 2 && event.getButton() == MouseEvent.BUTTON1) {
    System.out.println("double clicked");
  }
}
Cyaneous answered 29/10, 2010 at 12:9 Comment(2)
You'll probably want to check event.getButton() == MouseEvent.BUTTON1 as well, to only count double-clicks with the left mouse button.Fragment
@SeanVanGorder or SwingUtilities.isLeftMouseButton(event)Frances
R
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:

Rattray answered 29/10, 2010 at 12:16 Comment(0)
F
23

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.

Fukien answered 24/9, 2013 at 19:48 Comment(8)
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.Debug
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.Fukien
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.Debug
It wasn't very clear, please excuse me :) But your code is correct if the user is not sneaky ;)Debug
@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)Fukien
You're welcome ! See you in another discussion, maybe.Debug
And do you belief this ... such a low quality question ... and still more upvotes than any of mine. hrmpf.Jihad
What about checking e.getClickCount() % 2 == 0 to detect multiple double clicks within "awt.multiClickInterval"?Frances
B
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);
        }
    }
});
Bushelman answered 31/3, 2019 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.