Solved the problem by rewriting my complete GUI and Timeline. Don't know why it didn't work before, but now it is working.
Timeline is not running infinitely (JavaFx)
Question
I used to have a simple, timed (more-or-less) infinite loop:
while (!canrun) {
do_stuff(); //simple calculation
update_gui(); //updates some labels
Thread.sleep(waittime);
}
Which, naturally freezes the JavaFX-Application until it is finished with all calculations (canrun is set to false).
I replaced it with a timeline:
event = new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
do_stuff();
update_gui();
}
};
keyframe = new KeyFrame(Duration.millis(waittime), event);
timeline = new Timeline(Timeline.INDEFINITE, keyframe);
do_stuff() looks like this:
public void do_stuff() {
do_the_actual_stuff();
if (stuff_finished) timeline.stop();
}
(waittime is here a number between 1 and 1000 (ms)). On Buttonclick I start it with timeline.play(), and in do_stuff() I stop it when the calculations are done with timeline.stop().
I also have a function to change the waittime (even when it runs):
public void changewaittime() {
if (!(timeline.getStatus() == Animation.Status.RUNNING)) {
keyframe = new KeyFrame(Duration.millis(waittime), event);
timeline = new Timeline(Timeline.INDEFINITE, keyframe);
} else {
timeline.stop();
keyframe = new KeyFrame(Duration.millis(waittime), event);
timeline = new Timeline(Timeline.INDEFINITE, keyframe);
timeline.play();
}
}
And now my problem is, the Timeline only runs once, and not continuously, it doesn't even enter do_stuff() again, only if I call timeline.play() again. Even directly calling timeline.setCycleCount(Timeline.INDEFINITE) doesn't help.
Anything I missed?
Edit: I was unable to find my mistake, so I rewrote the complete GUI and now it is working.
Solution 2
OTHER TIPS
In the Timeline constructor you are setting the frame rate using a constant meant for cycle count.
Use http://docs.oracle.com/javafx/2/api/javafx/animation/Animation.html#setCycleCount(int).