Distinguish between a single click and a double click in Java

Posted by user552279 on Stack Overflow See other posts from Stack Overflow or by user552279
Published on 2011-01-02T06:26:15Z Indexed on 2011/01/02 6:54 UTC
Read the original article Hit count: 184

Filed under:

Hi, 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!

© Stack Overflow or respective owner

Related posts about java