92

In Swing you can simply use setDefaultCloseOperation() to shut down the entire application when the window is closed.

However in JavaFX I can't find an equivalent. I have multiple windows open and I want to close the entire application if a window is closed. What is the way to do that in JavaFX?

Edit:

I understand that I can override setOnCloseRequest() to perform some operation on window close. The question is what operation should be performed to terminate the entire application?

stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        stop();
    }
});

The stop() method defined in Application class does nothing.

13 Answers 13

107

The application automatically stops when the last Stage is closed. At this moment, the stop() method of your Application class is called, so you don't need an equivalent to setDefaultCloseOperation()

If you want to stop the application before that, you can call Platform.exit(), for example in your onCloseRequest call.

You can have all these information on the javadoc page of Application : http://docs.oracle.com/javafx/2/api/javafx/application/Application.html

2
  • 11
    For reference (as mentioned in the linked javadoc page): The application is only stopped automatically if the implicitExit attribute on Platform is set to true. Commented May 13, 2015 at 10:00
  • 1
    FWIW stopping the stage will not terminate the app if the stage was never shown. Commented Jun 1, 2023 at 19:31
85

Some of the provided answers did not work for me (javaw.exe still running after closing the window) or, eclipse showed an exception after the application was closed.

On the other hand, this works perfectly:

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent t) {
        Platform.exit();
        System.exit(0);
    }
});
5
  • Were you doing something multi threaded? Seems to me that it's not shutting down (for me) when using ExecutorService (which might be intended behavior, I haven't checked the docs yet).
    – Manius
    Commented Oct 8, 2017 at 3:36
  • 3
    For me, adding the System.exit(0); was the only thing that made the process terminate after the main window closed.
    – Johannes
    Commented Dec 22, 2017 at 17:28
  • 2
    If you like it short, this works in Java 8: theStage.setOnCloseRequest(e -> System.exit(0)); Commented Feb 6, 2019 at 7:03
  • 3
    If you start your own ExecutorService (i.e. a thread pool), as @Manius did, then you need to explicitly shut it down (if the threads a non-daemon). You can do this in the #stop() method of your application class, if you want. However, JavaFX does not, on its own, manage ExecutorService instances (or any other thread-creating concurrency utility).
    – Slaw
    Commented Jan 17, 2020 at 19:13
  • By adding System.exit(0) a lot of my code in the stop method wasn't able to complete and produced all sorts of errors. Platform.exit() is all you actually need.
    – trilogy
    Commented Jul 22, 2022 at 14:32
30

For reference, here is a minimal implementation using Java 8 :

@Override
public void start(Stage mainStage) throws Exception {

    Scene scene = new Scene(new Region());
    mainStage.setWidth(640);
    mainStage.setHeight(480);
    mainStage.setScene(scene);

    //this makes all stages close and the app exit when the main stage is closed
    mainStage.setOnCloseRequest(e -> Platform.exit());

    //add real stuff to the scene...
    //open secondary stages... etc...
}
25
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        Platform.exit();
        System.exit(0);
    }
});
4
  • 1
    I just want to point out that in this case the stop() method won't be called at all (at least with JavaFX 8 on windows) because System.exit() closes everything too early. In this case you might just call System.exit() only. But if you want to do something in the stop() method, I would recommend calling Platform.exit() here and System.exit(0) at the end of the stop() method if needed.
    – tomorrow
    Commented Jul 20, 2017 at 11:54
  • Some bad syntax here. You're missing a closing ')'
    – Nibb
    Commented May 14, 2019 at 14:54
  • @tomorrow yep, Platform.exit() is all you really need. The System.exit() does not actually occur after the "plaform exits". It happens at the same time. Most if not all of the code in the stop() method doesn't complete. There is no point in putting System.exit() in here.
    – trilogy
    Commented Jul 22, 2022 at 14:35
  • I guess you could also wrap the System.exit in a Platform.runLater
    – xeruf
    Commented Jul 10, 2024 at 11:28
4

Instead of playing around with onCloseRequest handlers or window events, I prefer calling Platform.setImplicitExit(true) the beginning of the application.

According to JavaDocs:

"If this attribute is true, the JavaFX runtime will implicitly shutdown when the last window is closed; the JavaFX launcher will call the Application.stop() method and terminate the JavaFX application thread."

Example:

@Override
void start(Stage primaryStage) {
    Platform.setImplicitExit(true)
    ...
    // create stage and scene
}
1
  • 3
    Nice, but at least in FX 12 it is enable by default. The default value is true. Commented Oct 22, 2019 at 13:50
3

Did you try this..setOnCloseRequest

setOnCloseRequest(EventHandler<WindowEvent> value)   

There is one example

1
  • it is best than stop() method?
    – Carbos
    Commented Jun 2, 2016 at 9:58
3

Using Java 8 this worked for me:

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Region());
    stage.setScene(scene);

    /* ... OTHER STUFF ... */

    stage.setOnCloseRequest(e -> {
        Platform.exit();
        System.exit(0);
    });
}
1

For me only following is working:

primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {

        Platform.exit();

        Thread start = new Thread(new Runnable() {
            @Override
            public void run() {
                //TODO Auto-generated method stub
                system.exit(0);     
            }
        });

        start.start();
    }
});
0

This seemed to work for me:

EventHandler<ActionEvent> quitHandler = quitEvent -> {

        System.exit(0);

    };
    // Set the handler on the Start/Resume button
    quit.setOnAction(quitHandler);
0

Try

 System.exit(0);

this should terminate thread main and end the main program

0

getContentPane.remove(jfxPanel);

try it (:

-1

in action button try this : stage.close();


exemple:

Stage stage =new Stage();

BorderPane root=new BorderPane();

Scene scene=new Scene();

Button b= new Button("name button");

       b.setOnAction(new EventHandler<ActionEvent>() {
   
       @Override
        public void handle(ActionEvent event) {

                 stage.close();
               
            }
            });

root.getChildren().add(b);

stage.setTitle("");

stage.setScene(scene);

stage.show();

1
  • Please try to clean up the code so readers can follow it. Some of the code is formatted as code, but the rest is not; please format all of the code as code and, if there is a break between sections, make this clear. Commented May 8, 2021 at 12:48
-2

You MUST override the "stop()" method in your Application instance to make it works. If you have overridden even empty "stop()" then the application shuts down gracefully after the last stage is closed (actually the last stage must be the primary stage to make it works completely as in supposed to be). No any additional Platform.exit or setOnCloseRequest calls are need in such case.

1
  • please supply a working example - didn't work for me Commented Aug 22, 2017 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.