I'm having difficulty finding out if this is even possible. The common behavior that most people want is to fade out an extension of a node
, which is entirely possible through a FadeTransition
However, I'm trying to fade out an entire stage, so imagine closing the running program and instead of simply killing the windows (i.e. showing -> not showing), I would like the window (stage
) to fade out over 2 seconds like a toast or notification would.
Create a timeline with a KeyFrame that changes the opacity of the stage's scene's root node. Also make sure to set the Stage style and the scene fill to transparent. Then make the program exit once the timeline is finished.
Below is an application with a single big button that, when clicked, will take 2 seconds to fade away, 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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With