JavaFX application still running after close

2019-02-16 06:57发布

I'm having problem to close my javaFX application, when I click the close button from my stage, my application disappears but if I look for it in my task manager my application still there without close. I've tried to use this code below to force it close the main thread and all childrens threads but the problem persists.

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {

            @Override
            public void handle(WindowEvent t) {
                Platform.exit();
            }

        });

8条回答
▲ chillily
2楼-- · 2019-02-16 07:06

Does your application spawn any child threads? If so have you ensured that you terminate them (assuming that they're not daemon threads)?

If your application spawns non-daemon threads then they (and therefore your app) will continue to live on until such time you kill the process

查看更多
Explosion°爆炸
3楼-- · 2019-02-16 07:10

I currently had this problem while using an ThreadExecutor in the controller. Application does not exit if the ThreadExecutor is not shutdown. See here: how-to-shut-down-all-executors-when-quitting-an-application

As it can be a problem to recognize an application exit in the controller, you can get a reference to the controller from your Application class like so (using the sample application from Eclipse):

public class Main extends Application {
private SampleController controller;

@Override
public void start(Stage primaryStage) {
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("MyFXML.fxml"));

        BorderPane root = (BorderPane)loader.load(getClass().getResource("Sample.fxml").openStream());

        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
        controller = loader.<SampleController>getController();          
    } 
    catch(Exception e) 
    {
        e.printStackTrace();
    }
}

Your Application overrides the stop method, where you can call a housekeeping method of the controller (i use a method called startHousekeeping):

/**
 * This method is called when the application should stop, 
 * and provides a convenient place to prepare for application exit and destroy resources. 
 */
@Override
public void stop() throws Exception 
{
    super.stop();
    if(controller != null)
    {
        controller.startHousekeeping(); 
    }

    Platform.exit();
    System.exit(0);
}
查看更多
Root(大扎)
4楼-- · 2019-02-16 07:13

You could close your application by clicking close Button , with some code.

      stage.setOnCloseRequest(
                event -> closeMyApp()                   
        );
      private void closeMyApp()
       {
         try
            {
            Stage stage = (Stage) closeButton.getScene().getWindow();
            stage.close();
            }
          catch(Exception ee)
              {
               ex.printStackTrace();
               }
      }
    // where closeButton is button having similar controller class initialization.
           @FXML
           private Button closeButton;
查看更多
Explosion°爆炸
5楼-- · 2019-02-16 07:16

I was able to fix this problem by calling com.sun.javafx.application.tkExit(). You can read more in my other answer here: https://stackoverflow.com/a/22997736/1768232 (these two questions really are duplicates).

查看更多
疯言疯语
6楼-- · 2019-02-16 07:17

The only way was to call System.exit(0);

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent t) {
                Platform.exit();
                System.exit(0);
            }
        });

[EDITED]

System.exit will just hide your application, if you open SO's manager task your application will be there. The correct way is to check your Threads, one by one and close all before close application.

查看更多
Lonely孤独者°
7楼-- · 2019-02-16 07:25

First Look Here

 public void start(Stage stage) {
        Platform.setImplicitExit(true);
        stage.setOnCloseRequest((ae) -> {
            Platform.exit();
            System.exit(0);
        });
}
查看更多
登录 后发表回答