Javafx 2 click and double click

前端 未结 9 1462
盖世英雄少女心
盖世英雄少女心 2020-12-01 06:00

I would like to know if it was possible to detect the double-click in JavaFX 2 ? and how ?

I would like to make different event between a click and a double click.

9条回答
  •  鱼传尺愫
    2020-12-01 06:39

    Yes you can detect single, double even multiple clicks:

    myNode.setOnMouseClicked(new EventHandler() {
        @Override
        public void handle(MouseEvent mouseEvent) {
            if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
                if(mouseEvent.getClickCount() == 2){
                    System.out.println("Double clicked");
                }
            }
        }
    });
    

    MouseButton.PRIMARY is used to determine if the left (commonly) mouse button is triggered the event. Read the api of getClickCount() to conclude that there maybe multiple click counts other than single or double. However I find it hard to distinguish between single and double click events. Because the first click count of the double click will rise a single event as well.

提交回复
热议问题