I am currently confronted to this problem: detect double clic in JavaFX.
What I want to do :
When you make a double click, you click twice, so this is what will occurs:
- The first click will be catch and the simpleClickAction will be done (because clickCount != 2) ---> I don't want it : this is a double click and I want the simpleClick action to be perform only on single click !,
- The second click will be catch and the double clickAction will be done --> This is what I want.
So what I want is :
- To perform a simpleClick action on single click only (and not those which are part of a double click),
- To perform a double click action on doubleClick only.
How I did it
In swing this is done by using timer. So why not do the same with a Timeline in JavaFX.
This is how I solve the problem (the code is also linked to this post) :
/* * Main.fx * * Created on 14 déc. 2008, 15:10:59 */ package fr.antoinj.detectdoubleclick; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.scene.Group; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.Scene; import javafx.scene.shape.Rectangle; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; /** * @author Jonathan */ var textContent = "Nothing detected"; var clickInTheLastFewMoments: Boolean = false; var lastLauchedTimeLine:Timeline ; var clickedTimeLine: Timeline= Timeline { keyFrames: [KeyFrame { time: 0s action:function(){ lastLauchedTimeLine.stop(); clickInTheLastFewMoments=true; } } , KeyFrame { time: 200ms action:function(){ clickInTheLastFewMoments=false; simpleClick(); } } ] } function simpleClick(){ textContent="Simple click detected"; } function doubleClick(){ textContent="Double click detected"; } Stage { title: "How I detect doubleClick in JavaFX" width: 400 height: 80 scene: Scene { content: Group{ content: [Rectangle { x: 75, y: 0 width: 400, height: 50 fill: Color.BLACK onMouseClicked: function(me:MouseEvent){ if(clickInTheLastFewMoments){ clickInTheLastFewMoments=false; clickedTimeLine.stop(); doubleClick(); }else { clickedTimeLine.playFromStart(); } } }, Rectangle { x: 0, y: 0 width: 75 height: 50 fill: Color.ORANGERED onMouseClicked: function(me:MouseEvent){ textContent="Nothing detected"; } }, Text { font: Font.font("Verdana",FontWeight.BOLD,12) fill: Color.BLACK x: 8 y: 25 content: "REFRESH" } , Text { font: Font.font("Verdana",FontWeight.MEDIUM,24) fill:Color.PINK x: 80 y: 30 content: bind textContent; }] } } }
You can also see a demo of this javaFx apps here : link to the demo page or launch this JNLP : link to the JNLP
My question is : Do you have a better way to do this ?