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);
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);
stage.setScene(scene);
stage.show();
}
public static void main (String[] args) {
launch();
}
}
source
share