JavaFX fades and closes

Itโ€™s hard for me to understand if this is possible. The usual behavior that most people require is to reduce expansion node, which is quite possible withFadeTransition

However, I am trying to fade away the whole stage, so imagine that you close the running program and instead of just killing the windows (i.e. show โ†’ not show), I would like the window ( stage) to disappear within 2 seconds like a toast or notice.

+4
source share
1 answer

Create a timeline with a KeyFrame that changes the opacity of the scene root of the scene node. Also make sure the scene style and scene fill are transparent. Then end the program after the timeline is completed.

Below is an application with one big button, which when pressed will take 2 seconds to disappear, and then the program will close.

public class StageFadeExample extends Application {

    @Override
    public void start(Stage arg0) throws Exception {
        Stage stage = new Stage();
        stage.initStyle(StageStyle.TRANSPARENT); //Removes window decorations

        Button close = new Button("Fade away");
        close.setOnAction((actionEvent) ->  {
            Timeline timeline = new Timeline();
            KeyFrame key = new KeyFrame(Duration.millis(2000),
                           new KeyValue (stage.getScene().getRoot().opacityProperty(), 0)); 
            timeline.getKeyFrames().add(key);   
            timeline.setOnFinished((ae) -> System.exit(1)); 
            timeline.play();
        });

        Scene scene = new Scene(close, 300, 300);
        scene.setFill(Color.TRANSPARENT); //Makes scene background transparent
        stage.setScene(scene);
        stage.show();
    }

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

Source: https://habr.com/ru/post/1599328/


All Articles