20

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

        });
Zephyr
  • 9,885
  • 4
  • 28
  • 63
Victor Laerte
  • 6,446
  • 13
  • 53
  • 102

7 Answers7

27

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

Sean Landsman
  • 7,033
  • 2
  • 29
  • 33
26

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.

Victor Laerte
  • 6,446
  • 13
  • 53
  • 102
  • 4
    That will only hide the underlying issue. The other answer is probably correct. – assylias Feb 18 '13 at 22:59
  • JavaFX can be launched through so many different ways. You might not always know which threads were alive prior to and after an invocation of Application.launch. So, question is: What be the harm if one want to kill the application completely and call System.exit()? – Martin Andersson Jun 20 '13 at 22:33
  • It is actually working to me. javaw.exe project's process is closed and Eclipse disables the Stop button, which instead remains active after closing without these calls – BlackBox Jul 15 '17 at 22:41
10

First Look Here

 public void start(Stage stage) {
        Platform.setImplicitExit(true);
        stage.setOnCloseRequest((ae) -> {
            Platform.exit();
            System.exit(0);
        });
}
Rajat
  • 2,467
  • 2
  • 29
  • 38
6

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);
}
Community
  • 1
  • 1
jausen brett
  • 1,111
  • 7
  • 10
5

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

Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158
  • 2
    I have read [this](http://meta.stackoverflow.com/a/253066/3375713) ranting meta answer of yours and thought your hard work should not go uncredited. So, I am doing all I can: +1 :-) – Sнаđошƒаӽ Feb 03 '16 at 09:50
  • 1
    There's no `com.sun.javafx.application.tkExit()` in the current JDK. – shinzou Sep 13 '16 at 19:18
3

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.

Isfirs
  • 124
  • 1
  • 12
1

To imitate pressing 'x' one can do:

stage.fireEvent(new WindowEvent(stage, WindowEvent.WINDOW_CLOSE_REQUEST))
ov7a
  • 1,497
  • 4
  • 15
  • 34