Felix Felix - 3 months ago 11
Java Question

Two actions when animation ends (setOnFinished)

I've been trying to perform two different actions to the same rectangle after animation is done: the rectangle must change color and the text inside of it must become visible. But the programme only performs the second event, ignoring the first one (even if I swithed them). How can I make it work?

public void open() {
RotateTransition trans = new RotateTransition(Duration.seconds(1), rec);
trans.setToAngle(180);
trans.setAxis(Rotate.Y_AXIS);
trans.setOnFinished (event -> tex.setVisible(true));
trans.setOnFinished (event -> rec.setFill(Color.WHITE));
trans.play();

Answer

Either:

  1. Just put them in the same event handler:

    trans.setOnFinished(event -> {
        text.setVisible(true);
        rec.setFill(Color.WHITE);
    });
    
  2. or add change listeners to the statusProperty:

    trans.statusProperty().addListener((obs, oldStatus, newStatus) -> {
        if (newStatus == Animation.Status.STOPPED) {
            text.setVisible(true);
        }
    });
    
    trans.statusProperty().addListener((obs, oldStatus, newStatus) -> {
        if (newStatus == Animation.Status.STOPPED) {
            rec.setFill(Color.WHITE);
        }
    });
    
Comments