Hauke
Hauke

Reputation: 1465

JavaFX Timeline Cycle Listener

is there a way to get the information when the next round of a cycle starts? The onFinish method of the timeline is not working for infinite cycles.

My only idea is to use the currentTimeProperty and check if the value increases or decreases.

Thanks for your help Hauke

Upvotes: 1

Views: 966

Answers (1)

fabian
fabian

Reputation: 82461

Add a event handler to the last KeyFrame (or modify a existing one) to run a event when the Timeline animation restarts, e.g. change

Timeline timeline = new Timeline(
        new KeyFrame(Duration.ZERO, new KeyValue(node.translateXProperty(), 0)),
        new KeyFrame(Duration.seconds(1), new KeyValue(node.translateXProperty(), 300))
);

timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();

to

Timeline timeline = new Timeline(
        new KeyFrame(Duration.ZERO, new KeyValue(node.translateXProperty(), 0)),
        new KeyFrame(Duration.seconds(1), evt -> System.out.println("finished"), new KeyValue(node.translateXProperty(), 300))
);

...

to print "finished" after every cycle.

Upvotes: 5

Related Questions