Reputation: 7255
A Stage
exist which contains some simple nodes like a Circle
(RIGHT) and a Rectangle
(LEFT) in a BordePane
Layout.
What I want is if the mouse has not been moved for 1 second (in the window) the circle to disappear.
If the elememt has not entered Rectangle
during the last 1 second the rectangle to disappear.
I am thinking of using a Thread
but it doesn't seem right.
Is there any JavaFX
packages to do this?Some code would be grateful.
Upvotes: 0
Views: 784
Reputation: 209225
Use a couple of PauseTransition
s:
PauseTransition hideCircle = new PauseTransition(Duration.seconds(1));
PauseTransition hideRectangle = new PauseTransition(Duration.seconds(1));
// restart hideCircle if mouse moves in window:
scene.addEventFilter(MouseEvent.MOUSE_MOVED, e -> hideCircle.playFromStart());
// restart hideRectangle if mouse enters rectangle:
rectangle.setOnMouseEntered(e -> hideRectangle.playFromStart());
// hide circle if pause gets to end:
hideCircle.setOnFinished(e -> circle.setVisible(false));
// hide rectangle if pause gets to end:
hideRectangle.setOnFinished(e -> rectangle.setVisible(false));
// start "timers":
hideCircle.play();
hideRectangle.play();
You might prefer to remove the shapes from their parent instead of setting visible
to false, depending on exactly what you want to do.
Upvotes: 3
Reputation: 880
Use javafx.concurrent.Task<V>
(doc: https://docs.oracle.com/javafx/2/api/javafx/concurrent/Task.html) to implement a background listener that would react to your changes, once you want to do something with the GUI elements do it in Platform.runLater()
Upvotes: 0