88

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.

Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169

13 Answers13

106

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

SpaceCore186
  • 586
  • 1
  • 8
  • 22
Teocali
  • 2,725
  • 2
  • 23
  • 39
  • 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`. – Rahel Lüthy May 13 '15 at 10:00
  • 1
    FWIW stopping the stage will not terminate the app if the stage was never shown. – Nicholas Miller Jun 01 '23 at 19:31
84

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);
    }
});
Neuron
  • 5,141
  • 5
  • 38
  • 59
Cyrus13
  • 897
  • 6
  • 7
  • 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 Oct 08 '17 at 03:36
  • 3
    For me, adding the `System.exit(0);` was the only thing that made the process terminate after the main window closed. – Johannes Dec 22 '17 at 17:28
  • 2
    If you like it short, this works in Java 8: theStage.setOnCloseRequest(e -> System.exit(0)); – Jaco Van Niekerk Feb 06 '19 at 07: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 Jan 17 '20 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 Jul 22 '22 at 14:32
29

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...
}
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105
24
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        Platform.exit();
        System.exit(0);
    }
});
Om Prakash
  • 793
  • 7
  • 14
  • 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 Jul 20 '17 at 11:54
  • Some bad syntax here. You're missing a closing ')' – Nibb May 14 '19 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 Jul 22 '22 at 14:35
3

Did you try this..setOnCloseRequest

setOnCloseRequest(EventHandler<WindowEvent> value)   

There is one example

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
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);
    });
}
madx
  • 6,723
  • 4
  • 55
  • 59
3

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
}
Daniel Ferber
  • 301
  • 1
  • 7
  • 2
    Nice, but at least in [FX 12](https://openjfx.io/javadoc/12/javafx.graphics/javafx/application/Platform.html#setImplicitExit(boolean)) it is enable by default. `The default value is true.` – Benjamin Peter Oct 22 '19 at 13:50
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();
    }
});
Pochmurnik
  • 780
  • 6
  • 18
  • 35
Piyush Aghera
  • 965
  • 7
  • 13
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);
Eddy Zavala
  • 549
  • 1
  • 4
  • 12
0

Try

 System.exit(0);

this should terminate thread main and end the main program

DENNIS KITHINJI
  • 217
  • 2
  • 14
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();

  • 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. – c.fogelklou May 08 '21 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.

Sap
  • 914
  • 1
  • 6
  • 20