How to stop transition when checkbox is unchecked javafx

Viewed 67

So I've made a checkbox that applies a scale transition to a rectangle when checked. But the problem is that the transition keeps going even after I uncheck the checkbox. Any ideas on how to make it stop after un-checking?

checkbox.setOnAction(e -> {
            ScaleTransition scaleT = new ScaleTransition(Duration.seconds(5), rectangle);
            scaleT.setAutoReverse(true);
            scaleT.setCycleCount(Timeline.INDEFINITE);
            scaleT.setToX(2);
            scaleT.setToY(2);
            scaleT.play();
        });
2 Answers

To control the animation, you need to define the transistion(with INDEFINITE cycle count) outside the CheckBox listener/action. Then you can just play/pause the animation as you required.

Below is the quick demo:

enter image description here

import javafx.animation.ScaleTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ScaleTransitionDemo extends Application {
    @Override
    public void start(Stage stage) {
        Shape rectangle = new Rectangle(50, 50, Color.BLUE);
        ScaleTransition transition = new ScaleTransition(Duration.seconds(1), rectangle);
        transition.setDuration(Duration.seconds(1));
        transition.setAutoReverse(true);
        transition.setCycleCount(Timeline.INDEFINITE);
        transition.setToX(3);
        transition.setToY(3);

        CheckBox checkBox = new CheckBox("Animate");
        checkBox.selectedProperty().addListener((obs, old, selected) -> {
            if (selected) {
                transition.play();
            } else {
                transition.pause();
            }
        });

        StackPane pane = new StackPane(rectangle);
        VBox.setVgrow(pane, Priority.ALWAYS);
        VBox root = new VBox(20, checkBox, pane);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 300, 300);
        stage.setScene(scene);
        stage.setTitle("Scale transition");
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

stopping scale transition javafx

checking whether checkbox is selected or not with .isSelected() method . In this approach , scaled node will back to xy = 1 scale if checkbox is unchecked , but it will be disabled until transition ends .You can adjust setDuration . I've changed it just for gif recording. This is a single class javafx app you can try .

App.java

public class App extends Application {

    @Override
    public void start(Stage stage) {
        Shape rectangle = new Rectangle(50, 50, Color.BLUE);
        ScaleTransition scaleT = new ScaleTransition(Duration.seconds(1), rectangle);

        CheckBox checkBox = new CheckBox("scale");
        checkBox.setOnAction(e -> {

            if (checkBox.isSelected()) {

                scaleT.setDuration(Duration.seconds(1));
                scaleT.setAutoReverse(true);
                scaleT.setCycleCount(Timeline.INDEFINITE);
                scaleT.setToX(2);
                scaleT.setToY(2);
                scaleT.play();
            } else {

                scaleT.setDuration(scaleT.getCurrentTime());
                scaleT.stop();
                scaleT.setCycleCount(1);
                scaleT.setToX(1);
                scaleT.setToY(1);
                scaleT.play();
                checkBox.setDisable(true);
                scaleT.setOnFinished((t) -> {
                    checkBox.setDisable(false);
                });

            }
        });

        var scene = new Scene(new HBox(50, rectangle, checkBox), 640, 480);
        stage.setScene(scene);
        stage.setTitle("scale transition");
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

    }
Related