GOXR3PLUS
GOXR3PLUS

Reputation: 7255

JavaFX hide Node after some duration

A Stage exist which contains some simple nodes like a Circle(RIGHT) and a Rectangle(LEFT) in a BordePane Layout.

  1. What I want is if the mouse has not been moved for 1 second (in the window) the circle to disappear.

  2. 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

Answers (2)

James_D
James_D

Reputation: 209225

Use a couple of PauseTransitions:

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

Stugal
Stugal

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

Related Questions