Mouse event with double click in java

让人想犯罪 __ 提交于 2019-11-29 09:14:45
Johnny Rocket

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.
}

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.

    private void jEditorPane3MouseClicked(java.awt.event.MouseEvent evt) {                                          

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!