I'm really beginner with javaFX and probably I'm missing something, but here my issue: I should show some changes in my gui application sequentially.
My code looks like this (each method performs a change in the gui, but the final result is given by the last method of course):
public void updateGui()
{
Platform.runLater(() -> {
doFirstStuff();
doSecondStuff();
doThirdStuff();
});
}
It would be nice if between the execution of each method there was a little delay (1,2 or maybe 3 seconds) for give time to the user to see what happened in the application.
My attempt was put:
Thread.sleep(1000);
after each method call, but since the thread is the JavaFX Application Thread, the result is awful.
Another solution that comes up right now in my mind is:
execute each method in another thread with a Runnable like this:
() -> {
Platform.runLater(() -> {
doFirstStuff();
});
Thread.sleep(1000);
});
and in updateGui()
submit the task in a newSingleThreadExecutor()
and wait for the termination of each task. But I don't know, it doesn't seem a good solution.
What is the best way to achieve my goal?
The simplest way is just to use a
Timeline
:I am assuming here that
updateGui()
is being called on a background thread (otherwise just removePlatform.runLater(...)
and calltimeline.play()
directly).If you really want to get your hands dirty with threads, you could achieve the same (more or less) with
but I think for use cases like this, the
Timeline
(or other parts of the animation API) are much cleaner (and use fewer resources, since they don't create a new thread).