20

By default MouseClicked event starts with one click. I have one in a JTextPane but I want to start with double click. Is it possible?

2
  • "Mouseclicked event starts with one click" makes no sense: mouseClicked events are dispatched whenever matching pressed/released are detected, for any number of clicks. What exactly are you trying to do? Commented Dec 7, 2011 at 9:07
  • yeah, I understand that, technically, but what's the problem? simply use @Johnny Rocket 's answer ... Commented Dec 7, 2011 at 10:08

5 Answers 5

56

I believe you can extract the click count from the MouseEvent (assuming its called e)

Try this

if (e.getClickCount() == 2 && !e.isConsumed()) {
     e.consume();
     //handle double click event.
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a good way to reset the click count so that the user can keep double-clicking and have every second click count as a double-click without a pause between?
@mouseas You could simply do if(e.getClickCount() % 2 == 0) to achieve that effect
4

I don't think there will be a solution to this, since Java can run on non-pc devices.

Most portable devices don't support double-click.

You may keep track of the moment of each mouse click and fire your own "double-click" event. But I don't think this is a good idea.

1 Comment

hmm .. where do you see mobile devices come into play?
3
    private void jEditorPane3MouseClicked(java.awt.event.MouseEvent evt) {                                          

            if (evt.getClickCount() == 2 && !evt.isConsumed()) {
                    evt.consume();
                    System.out.println("Double Click");
            }
    }

Comments

0

You can override the mousePressed() or mouseReleased() methods and asking if e.getClickCount() == 2 , I recommend using the mousePressed() or mouseReleased() instead of mouseClicked() method since using those will give the user more time to perform the clicks.

Comments

0

You can compute the time lapsed between consecutive clicks. Compare it with a threshold value and decide yourself whether it is a double click or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.