Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the close event of a stage in JavaFX?

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)");
       //}
   }
like image 939
java baba Avatar asked Mar 22 '14 10:03

java baba


People also ask

What does JavaFX stage stage do?

A Stage in JavaFX is a top-level container that hosts a Scene, which consists of visual elements. The Stage class in the javafx. stage package represents a stage in a JavaFX application. The primary stage is created by the platform and passed to the start(Stage s) method of the Application class.

What method is invoked to display a stage in JavaFX?

Showing a Stage The difference between the JavaFX Stage methods show() and showAndWait() is, that show() makes the Stage visible and the exits the show() method immediately, whereas the showAndWait() shows the Stage object and then blocks (stays inside the showAndWait() method) until the Stage is closed.

How do you get the JavaFX stage?

Accessing the Stage in JavaFX can be achieved by navigating the scene graph using the getScene() and getWindow() methods. As Stage extends Window , the resulting Window can be cast to Stage , where needed.

How JavaFX handle events?

In JavaFX applications, events are notifications that something has happened. As a user clicks a button, presses a key, moves a mouse, or performs other actions, events are dispatched. Registered event filters and event handlers within the application receive the event and provide a response.


3 Answers

I got the answer for this question

stage.setOnHiding(new EventHandler<WindowEvent>() {

         @Override
         public void handle(WindowEvent event) {
             Platform.runLater(new Runnable() {

                 @Override
                 public void run() {
                     System.out.println("Application Closed by click to Close Button(X)");
                     System.exit(0);
                 }
             });
         }
     });
like image 166
java baba Avatar answered Oct 18 '22 20:10

java baba


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:

The JavaFX runtime does the following, in order, whenever an application is launched:

  1. Constructs an instance of the specified Application class
  2. Calls the init() method
  3. Calls the start(javafx.stage.Stage) method
  4. Waits for the application to finish, which happens when either of the following occur:
    • the application calls Platform.exit()
    • the last window has been closed and the implicitExit attribute on Platform is true
  5. Calls the stop() method

As a result you simply override stop()

@Override
public void stop(){
    System.out.println("Stage is closing");
}
like image 27
waylonion Avatar answered Oct 18 '22 21:10

waylonion


stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
      public void handle(WindowEvent we) {
          System.out.println("Stage is closing");
      }
  }); 
like image 8
Sijo Jose Avatar answered Oct 18 '22 20:10

Sijo Jose