Victor Laerte
Victor Laerte

Reputation: 6556

JavaFX application still running after close

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();
            }

        });

Upvotes: 21

Views: 40763

Answers (7)

Rajat Verma
Rajat Verma

Reputation: 2590

First Look Here

 public void start(Stage stage) {
        Platform.setImplicitExit(true);
        stage.setOnCloseRequest((ae) -> {
            Platform.exit();
            System.exit(0);
        });
}

Upvotes: 10

ov7a
ov7a

Reputation: 1594

To imitate pressing 'x' one can do:

stage.fireEvent(new WindowEvent(stage, WindowEvent.WINDOW_CLOSE_REQUEST))

Upvotes: 1

jausen brett
jausen brett

Reputation: 1128

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);
}

Upvotes: 6

Isfirs
Isfirs

Reputation: 134

Just a note: Try checking if you use

Platform.setImplicitExit(false);

Had a similar problem and overflowing my tasks. The above line will not make the stage close, it will hide it.

Upvotes: 3

durron597
durron597

Reputation: 32323

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).

Upvotes: 5

Sean Landsman
Sean Landsman

Reputation: 7179

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

Upvotes: 28

Victor Laerte
Victor Laerte

Reputation: 6556

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.

Upvotes: 27

Related Questions