13

I search the forum and see this codes:

            public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                System.out.println("  and it's a double click!");
                wasDoubleClick = true;
            } else {
                Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty(
                        "awt.multiClickInterval");
                timer = new Timer(timerinterval.intValue(), new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        if (wasDoubleClick) {
                            wasDoubleClick = false; // reset flag
                        } else {
                            System.out.println("  and it's a simple click!");
                        }
                    }
                });
                timer.setRepeats(false);

                timer.start();
            }

        }

but the code runs incorrectly(Sometime it prints out " and it's a single click!" 2 times . It should print out " and it's a double click!"). Can anybody show me why? or can you give me some better ways to do this? Thank you!

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
Xitrum
  • 7,765
  • 26
  • 90
  • 126
  • can i ask- what exactly is the above code trying to accomplish? i.e. what is this application for? wanna get a better idea of what you're trying to accomplish.. – Dhruv Gairola Jan 02 '11 at 06:50
  • Related (reference): http://stackoverflow.com/questions/548180/java-ignore-single-click-on-double-click?rq=1 – Jayan Jun 28 '13 at 16:36

2 Answers2

21

Sometime it prints out " and it's a single click!" 2 times . It should print out " and it's a double click!").

That is normal. A double click only happens if you click twice within the specified time interval. So sometimes if you don't click fast enough you will get two single clicks in a row.

Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"); 

The above line of code determines how fast the double click must be.

For what its worth here is some code I have used to do the same thing. Don't know if its any better or worse than the code you have:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class ClickListener extends MouseAdapter implements ActionListener
{
    private final static int clickInterval = (Integer)Toolkit.getDefaultToolkit().
        getDesktopProperty("awt.multiClickInterval");

    MouseEvent lastEvent;
    Timer timer;

    public ClickListener()
    {
        this(clickInterval);
    }

    public ClickListener(int delay)
    {
        timer = new Timer( delay, this);
    }

    public void mouseClicked (MouseEvent e)
    {
        if (e.getClickCount() > 2) return;

        lastEvent = e;

        if (timer.isRunning())
        {
            timer.stop();
            doubleClick( lastEvent );
        }
        else
        {
            timer.restart();
        }
    }

    public void actionPerformed(ActionEvent e)
    {
        timer.stop();
        singleClick( lastEvent );
    }

    public void singleClick(MouseEvent e) {}
    public void doubleClick(MouseEvent e) {}

    public static void main(String[] args)
    {
        JFrame frame = new JFrame( "Double Click Test" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.addMouseListener( new ClickListener()
        {
            public void singleClick(MouseEvent e)
            {
                System.out.println("single");
            }

            public void doubleClick(MouseEvent e)
            {
                System.out.println("double");
            }
        });
        frame.setSize(200, 200);
        frame.setVisible(true);
     }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • The double click speed for Ubuntu Linux was so fast that I found it unusable (when I concentrated on trying to make a double click fast enough, the mouse would often move slightly, producing two single clicks). For that reason I would avoid using actions based on double click events if practical. – Andrew Thompson Jan 02 '11 at 07:01
  • 2
    Good point about the mouse moving a pixel or two which causes the click count to reset. In general I also don't recommend this approach as you should never really ignore a single click. The single click should be used to "select" something and the double click is then used to perform and "action" on the selected item. The class I posted does allow you to control the double click speed to make it more usable if you want. – camickr Jan 02 '11 at 07:07
  • 3
    (arguably, of course :-) you shouldn't interfere with OS generated behaviour: distinguish double vs single is the task of the underlying system. If that decides it's not a double-click, be it so. Otherwise, users have to adjust their clicking-speed-behaviour per application – kleopatra May 04 '11 at 09:47
  • @Andrew Thompson: regarding Linux double click interval: `#fix java mouse double-click speed touch /home/$USERNAME/.Xresources echo "*multiClickTime: 500" >> /home/$USERNAME/.Xresources xrdb ~/.Xresources` – MountainX Dec 26 '11 at 21:22
0
 public void mouseClicked(MouseEvent evt) { 
   if (evt.getButton()==MouseEvent.BUTTON1){
    leftClick = true; clickCount = 0;
    if(evt.getClickCount() == 2) doubleClick=true;
    Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

               timer = new Timer(timerinterval, new ActionListener() {
                public void actionPerformed(ActionEvent evt) {  
                    if(doubleClick){
                        System.out.println("double click.");
                        sb = new StringBuffer();
                        sb.append("Double Click");
                        clickCount++;
                        if(clickCount == 2){                               
                            clickCount=0;
                            doubleClick = false;
                        }

                    } else {

                        sb = new StringBuffer();
                        sb.append("Left Mouse");
                        System.out.println("single click.");                           
                    }
                }               
            });
            timer.setRepeats(false);
            timer.start();
            if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop();
}           
Anonymous
  • 152
  • 1
  • 1
  • 11
  • @Juno--I realize that this thread is almost a year old, so maybe I shouldn't even ask... but doesn't your code print "double click." twice when user double-clicks? I don't know if that was your intent or the implications of the situation. It just doesn't seem efficient, but I surely may be missing the point. – DSlomer64 Jun 17 '14 at 21:12
  • @DSlomer64 Thanks for your comment. Thats may be my mistake. I test mouse click events for my project which need to capture when single click event occour or double click occour. System.out.print is testing for click event. sb.append("") is the requirements of my project. Whatever thanks you. – Anonymous Jun 18 '14 at 03:06