Javafx 2 click and double click Javafx 2 click and double click java java

Javafx 2 click and double click


Yes you can detect single, double even multiple clicks:

myNode.setOnMouseClicked(new EventHandler<MouseEvent>() {    @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.


Here is another piece of code which can be used if you have to distinguish between a single- and a double-click and have to take a specific action in either case.

import java.util.concurrent.ScheduledFuture;import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.TimeUnit;import javafx.application.Application;import javafx.event.EventHandler;import javafx.scene.Scene;import javafx.scene.input.MouseButton;import javafx.scene.input.MouseEvent;import javafx.scene.layout.StackPane;import javafx.stage.Stage;public class DoubleClickDetectionTest extends Application {    boolean dragFlag = false;    int clickCounter = 0;    ScheduledThreadPoolExecutor executor;    ScheduledFuture<?> scheduledFuture;    public DoubleClickDetectionTest() {        executor = new ScheduledThreadPoolExecutor(1);        executor.setRemoveOnCancelPolicy(true);    }    public static void main(String[] args) {        launch(args);    }    @Override    public void start(Stage primaryStage) throws Exception {        StackPane root = new StackPane();        primaryStage.setScene(new Scene(root, 400, 400));        primaryStage.show();        root.setOnMouseDragged(new EventHandler<MouseEvent>() {            @Override            public void handle(MouseEvent e) {                if (e.getButton().equals(MouseButton.PRIMARY)) {                    dragFlag = true;                }            }        });        root.setOnMouseClicked(new EventHandler<MouseEvent>() {            @Override            public void handle(MouseEvent e) {                if (e.getButton().equals(MouseButton.PRIMARY)) {                    if (!dragFlag) {                        System.out.println(++clickCounter + " " + e.getClickCount());                        if (e.getClickCount() == 1) {                            scheduledFuture = executor.schedule(() -> singleClickAction(), 500, TimeUnit.MILLISECONDS);                        } else if (e.getClickCount() > 1) {                            if (scheduledFuture != null && !scheduledFuture.isCancelled() && !scheduledFuture.isDone()) {                                scheduledFuture.cancel(false);                                doubleClickAction();                            }                        }                    }                    dragFlag = false;                }            }        });    }    @Override    public void stop() {        executor.shutdown();    }    private void singleClickAction() {        System.out.println("Single-click action executed.");    }    private void doubleClickAction() {        System.out.println("Double-click action executed.");    }}


The response by P. Pandey is the simplest approach which actually distinguishes between single and double click, but it did not work for me. For one, the function "currentTimeMillis" already returns milliseconds, so dividing it by 1000 does not seem to be necessary. The version below worked for me in a more consistent fashion.

 @Override public void handle(MouseEvent t) {        long diff = 0;        currentTime=System.currentTimeMillis();        if(lastTime!=0 && currentTime!=0){            diff=currentTime-lastTime;            if( diff<=215)                isdblClicked=true;            else                isdblClicked=false;        }        lastTime=currentTime;        System.out.println("IsDblClicked()"+isdblClicked);        //use the isdblClicked flag...   }