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

My scene consists only of an ImageView displaying an image. I would like to change the image to black (the assigned color of the scene), and then after a while it will disappear again from black to the image. I found FadeTransition very suitable for this purpose. This is part of my 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();

My problem is that it ft1.play()is asynchronous, so the code below will start to execute before exiting ft1.play(). As a result, I see only the second transition. How can I wait for the first transition to complete and then launch the second transition? I can not put the thread to sleep between them, because this is the main javafx thread (tried and did not work).

onFinishedProperty() , while . :

    boolean isTransitionPlaying;
    FadeTransition ft = new FadeTransition(Duration.millis(2000), iv);
    ft.setFromValue(1.0);
    ft.setToValue(0.0);
    ft.onFinishedProperty().set(new EventHandler<ActionEvent>() {
        @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();

?

+5
3

( Thread.sleep) JavaFX - - , , , - . FX, , JavaFX . , , , .

Uluk ( ) SequentialTransition . : SequentialTransition, onFinished SequentialTransition, .

+3

, ft2 ft1,

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

ft2. ft2 ft1,

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

    @Override
    public void handle(ActionEvent event) {
        ft2.play();
    }
});
+17

I had a problem when another code was doing some calculations, and I wanted to run the animation in a JavaFX application, but I needed to do different code to wait for the animation to finish. I could not talk about this other code when the animation ended, so I created a method to play the animation, and then waited for it to complete:

    private synchronized void playAnimationAndWaitForFinish(final Animation animation) {
    if (Platform.isFxApplicationThread()) {
        throw new IllegalThreadStateException("Cannot be executed on main JavaFX thread");
    }
    final Thread currentThread = Thread.currentThread();
    final EventHandler<ActionEvent> originalOnFinished = animation.getOnFinished();
    animation.setOnFinished(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            if (originalOnFinished != null) {
                originalOnFinished.handle(event);
            }
            synchronized (currentThread) {
                currentThread.notify();
            }
        }
    });
    Platform.runLater(new Runnable() {

        @Override
        public void run() {
            animation.play();
        }
    });
    synchronized (currentThread) {
        try {
            currentThread.wait();
        } catch (InterruptedException ex) {
            //somebody interrupted me, OK
        }
    }
}

This method is not required to be called in the main JavaFX thread, otherwise it works for me.

+1
source

All Articles