This is what I have done so far, it works just fine, but when I double click, the double click event is fired and the single click event is fired, when I single click only the single click is fired of course.
long currentMillis = System.currentTimeMillis();
addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
long currentClickedMillis = System.currentTimeMillis();
long diffMillis = currentClickedMillis - currentMillis;
currentMillis = currentClickedMillis;
if (diffMillis > 500) {
fireEvent(new FlightListEvents(FlightListEvents.FLIGHT_BUTTON_CLICKED, this.aircraft));
} else {
fireEvent(new FlightListEvents(FlightListEvents.FLIGHT_BUTTON_DOUBLE_CLICKED, this.aircraft));
}
});
The other solutions on Stack Overflow do not work! They just count numbers of clicks and still have the same problem I have now.
I had the same problem once and couldn't find the solution too, so I had to develop my own solution.
For that I used Javafx's Timeline which is gonna give you the ability to start a time line at your first click and depending on if you afterward make a second click or not, it is gonna consider it to be either a single click or a double click, but never both at the same time.
Here is my solution:
// initialize your timeline
final Timeline clickTimeline = new Timeline();
// minimum duration of a double click 500ms
Duration clickDuration = Duration.millis(500);
KeyFrame clickKeyFrame = new KeyFrame(clickDuration);
boolean isClickTimelinePlaying = false;
clickTimeline.getKeyFrames().add(clickKeyFrame);
addEventFilter(MouseEvent.MOUSE_CLICKED, e -> {
if (isClickTimelinePlaying) {
// when clicking the second time before the time line finishes
isClickTimelinePlaying = false;
fireEvent(new FlightListEvents(FlightListEvents.FLIGHT_BUTTON_DOUBLE_CLICKED, this.aircraft));
clickTimeline.stop();
} else {
// when clicking for the first time
isClickTimelinePlaying = true;
// start the timeline
clickTimeline.play();
});
}
// if timeline finises without receiving a second click, consider it a single click
clickTimeline.setOnFinished(event -> {
fireEvent(new FlightListEvents(FlightListEvents.FLIGHT_BUTTON_CLICKED, this.aircraft));
isClickTimelinePlaying = false;
});