In JavaFX, how can I get the event if a user clicks the Close Button(X) (right most top cross) a stage?
I want my application to print a debug message when the window is closed. (System.out.println("Application Close by click to Close Button(X)")
)
@Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
// Any Event Handler
//{
System.out.println("Application(primaryStage) Closed by click to Close Button(X)");
//}
}
Another method for achieving the same effect, but remains more consistent with the way you start your application is to override stop();
According to the JavaFX documentation, the lifecycle of an instance of an Application is as follows:
As a result you simply override stop()
I got the answer for this question