Java : ignore single click on double click?

Dave Carpeneto :

can anyone think of a good way to ignore the single click that comes with a double-click in Java ?

I'm looking to have different behaviors for each such that:

  • single-click paints crosshairs on the click point
  • double-click selects an object on the screen, but should not paint crosshairs on the click point

... can anyone think of a way to do this ? Some sort of timer set-up maybe ? An ideas appreciated :-)

<disclaimer> ...and yes, I know I'm committing a most heinous usability / UI faux pas. </disclaimer>

EDIT #2:

Even though this works the delay due to the timer is maddening - I'm abandoning this solution, and using middle-click for selection instead of double-click...

EDIT:

Thanks cgull - this is what I was able to come up with given your confirmation that there's no easy way to do this (note that if I set the timer < 200 odd racing is seen between the click & the timer, but as long as I set this to a value > 200 things work just peachy) :

public void mouseClicked(MouseEvent e) {
    System.out.println( "Click at (" + e.getX() + ":" + e.getY() + ")" );
    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();
    }
}
cgull :

Indeed you'll need to set up a Timer in your overridden mouseClicked() method of your MouseAdapter to detect the time interval between the two clicks. The default interval in ms can be found by querying Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval"). If another mouse click is detected before the timer expires, then you have a double-click, else once the timer expires, you can process the single-click.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Distinguish between a single click and a double click in Java

single click and double click issue

Treat Single Click and Double Click as same

Single click blocks double click in tkinter

Separate action for single click and double click with RxJs

Single click in selenium acts as double click

execute either single click or double click . Not both

Why double click instead of single?

identifying double click in java

Close menu on overlay click requires double click instead of single click

click event works only on double click in stead of a single click

Prevent single click on WPF control when double click occured

Start Editing a TableCell on single click instead of double click

fancy box not open in single click,require double click

Double-click instead of single-click in Ubuntu 12.04

How to differentiate single click event and double click event?

how to trigger a double click on a single click using jquery

Changing <input type= color> behaviour from single click to double click

How to Make a Button Perform Single Click Instead of Double Click

Prevent default single click event on double click on a link in HTML

Change single click as double click to specific element (e.g 'a')

Single click and Double click on the same element, not working; Javascript

Mouse event with double click in java

Double Click Problem in android with java

Windows 10 Double-clicks on single click

Trouble with a single click on my button. Click event working only for double click

How to prevent a double execution of a single-click event when actually trying to trigger a double click event?

java - catch event of double click on icon in tray

Java .jar executable not opening (on double click)