0

How can I catch when user double clicks on the component?

 window.getComponent().addMouseListener(new MouseInputAdapter(){
            public void mouseClicked(final java.awt.event.MouseEvent evt) {
                Xpcom.invokeLater(new Runnable() {
                    public void run() {

                    }
                });                
            }
        })

;
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
KJW
  • 15,035
  • 47
  • 137
  • 243
  • 1
    There is no need to use invokeLater in an event listener. All eventlistener code already runs on the event dispatch thread. – camickr May 04 '11 at 04:31
  • no need for the sake of doing stuff on the EDT, but might be a need to do it at-the-end of whatever is happening :-) – kleopatra May 04 '11 at 09:48

2 Answers2

6

You'll have to use the getClickCount() of MouseEvent

if (evt.getClickCount() == 2)  // double click
{
    // do stuff
}
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • Note that this will also trigger a single click. The only way that I know to handle that is to delay the reaction to the single click for a number of milliseconds and cancel it when the double click comes in. – Peter Becker May 04 '11 at 05:02
  • @Peter - hmm ... what do you mean by "trigger a single click"? Anyway, a strong no for interfering with system behaviour: if the system decides that it is not a double, so be it. – kleopatra May 04 '11 at 09:52
  • This if statement will be executed on a double right click as well – cfogelberg Jul 18 '13 at 09:30
1

See the following post:

Distinguish between a single click and a double click in Java

Community
  • 1
  • 1
Peter Becker
  • 8,795
  • 7
  • 41
  • 64
  • Peter actually you were right. the problem is exactly that distinguishing between single click and double click. in my case it records 2 single clicks and then finally a double click. instead it should be just a single double click. – KJW May 13 '11 at 23:30