Mouse event with double click in java

后端 未结 5 2107
借酒劲吻你
借酒劲吻你 2020-12-18 19:49

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?

相关标签:
5条回答
  • 2020-12-18 20:28

    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.
    }
    
    0 讨论(0)
  • 2020-12-18 20:28
        private void jEditorPane3MouseClicked(java.awt.event.MouseEvent evt) {                                          
    
                if (evt.getClickCount() == 2 && !evt.isConsumed()) {
                        evt.consume();
                        System.out.println("Double Click");
                }
        }
    
    0 讨论(0)
  • 2020-12-18 20:32

    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.

    0 讨论(0)
  • 2020-12-18 20:34

    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.

    0 讨论(0)
  • 2020-12-18 20:38

    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.

    0 讨论(0)
提交回复
热议问题