Once Timeline is set up, you can control its flow using the following methods:
timeline.play();
timeline.pause();
timeline.stop();
Note that stop() resets Timeline, while pause() allows Timeline to be played again from the same point by another play() call.
Two other important properties are the following:
- autoReverse:Allows an animated object to go back and forth smoothly
- cycleCount: Repetition count. The default value is 1 and the maximalvalue is infinite; it can be set through the Timeline.INDEFINITE constant:
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(true);
To check whether Timeline is playing, you can use the Timeline.getStatus() method, which returns one of the following three values:
Animation.Status.RUNNING // after play()
Animation.Status.PAUSED // after pause()
Animation.Status.STOPPED // initial Timeline state and one after stop()
For example, to start and pause Timeline on mouse clicks, you can use the following code:
// Also note that is how mouse events are handled in JavaFX:
// you choose an object and provide a corresponding MouseEvent handler
root.setOnMouseClicked((event) -> {
if (timeline.getStatus() == Animation.Status.RUNNING)
timeline.pause();
else
timeline.play();});