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

后端 未结 3 1490
有刺的猬
有刺的猬 2021-02-19 03:09

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

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-19 03:53

    I got the answer for this question

    stage.setOnHiding(new EventHandler() {
    
             @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);
                     }
                 });
             }
         });
    

提交回复
热议问题