title: How to wait for a transition to end in javafx 2.1?
body: <p>My scene consists only of an <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/image/ImageView.html">ImageView</a>, 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 <a href="http://docs.oracle.com/javafx/2/api/javafx/animation/FadeTransition.html">FadeTransition</a> very fitting for this purpose. This is a piece of my code:</p>
  
  <pre><code>    // fade to black transition
      FadeTransition ft1 = new FadeTransition(Duration.millis(2000), myImageView);
      ft1.setFromValue(1.0);
      ft1.setToValue(0.0);
      ft1.play();
  
      // fade from black transition
      FadeTransition ft2 = new FadeTransition(Duration.millis(2000), myImageView);
      ft2.setFromValue(0.0);
      ft2.setToValue(1.0);
      ft2.play();
  </code></pre>
  
  <p>My problem is that <code>ft1.play()</code> is asynchronous, so the code below will start being executed before <code>ft1.play()</code> 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).</p>
  
  <p>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:</p>
  
  <pre><code>    boolean isTransitionPlaying;
      FadeTransition ft = new FadeTransition(Duration.millis(2000), iv);
      ft.setFromValue(1.0);
      ft.setToValue(0.0);
      ft.onFinishedProperty().set(new EventHandler&lt;ActionEvent&gt;() {
          @Override 
          public void handle(ActionEvent actionEvent) {
              transitionPlaying = false;
          }
      });
      transitionPlaying = true;
      ft.play();
  
      while (transitionPlaying == true)
      {
          // busy wait
          System.out.println("still waiting...");
      }
  
      FadeTransition ft2 = new FadeTransition(Duration.millis(2000), iv);
      ft2.setFromValue(0.0);
      ft2.setToValue(1.0);
      ft2.play();
  </code></pre>
  
  <p>How is waiting done properly? Thank you</p>