How to wait for a transition to end in javafx 2.1?


My scene consists only of an ImageView, displaying an image. I would like to fade the image to black (assigned color of the scene), then after some time, fade from black to the image again. I found the FadeTransition very fitting for this purpose. This is a piece of my code:

    // fade to black transition
    FadeTransition ft1 = new FadeTransition(Duration.millis(2000), myImageView);

    // fade from black transition
    FadeTransition ft2 = new FadeTransition(Duration.millis(2000), myImageView);

My problem is that is asynchronous, so the code below will start being executed before is exited. As the result I see only the second transition. How can I wait for the first transition to end and then to launch the second transition? I cannot put the thread to sleep in between because it's the main javafx thread (tried and didn't work).

I tried using the onFinishedProperty() method with the combination of a busy-waiting on a flag, but I get stuck in the while loop forever. Here is my code for that:

    boolean isTransitionPlaying;
    FadeTransition ft = new FadeTransition(Duration.millis(2000), iv);
    ft.onFinishedProperty().set(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent actionEvent) {
            transitionPlaying = false;
    transitionPlaying = true;;

    while (transitionPlaying == true)
        // busy wait
        System.out.println("still waiting...");

    FadeTransition ft2 = new FadeTransition(Duration.millis(2000), iv);

How is waiting done properly? Thank you

6/25/2012 10:57:43 AM

Accepted Answer

Busy waiting (or even Thread.sleep) on the JavaFX application thread is always a bad idea - you tie up the thread which handles the UI processing so your transitions, as well as the rest of your UI, is never updated - effectively freezing your app UI for the duration of the busy wait. For a responsive UI, you need to run your logic on the FX application thread as quickly as possible, then let the thread go so the rest of the JavaFX system can get on with it's processing. This is why the transitions have async callbacks - which, once you get used to them, are a very natural way of developing.

In addition to Uluk's solutions (which are great), you could also look at the SequentialTransition class for handling assistance in performing transitions in sequence. Note that if you want to take an action after the SequentialTransition has completed, you will still want to add an onFinished handler to the SequentialTransition to take action at that time.

6/25/2012 5:25:47 PM

Ok if your ft2 is the reflective animation of ft1 then do

// Or
// ft1.setCycleCount(Timeline.INDEFINITE);
// to loop infinitely (blinking effect) until stop()

and you don't ft2. If you still need ft2 to play after ft1 then

ft1.setOnFinished(new EventHandler<ActionEvent>() {

    public void handle(ActionEvent event) {;

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow