How to close a JavaFX application on window close?

JavaJavafxJavafx 2Javafx 8

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

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

Solution 5 - Java

Did you try this..setOnCloseRequest

setOnCloseRequest(EventHandler<WindowEvent> value)   

There is one example

Solution 6 - Java

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

Solution 7 - Java

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
}

Solution 8 - Java

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

Solution 9 - Java

This seemed to work for me:

EventHandler<ActionEvent> quitHandler = quitEvent -> {

		System.exit(0);

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

Solution 10 - Java

Try

 System.exit(0);

this should terminate thread main and end the main program

Solution 11 - Java

getContentPane.remove(jfxPanel);

try it (:

Solution 12 - Java

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

Solution 13 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKshitiz SharmaView Question on Stackoverflow
Solution 1 - JavaTeocaliView Answer on Stackoverflow
Solution 2 - JavaCyrus13View Answer on Stackoverflow
Solution 3 - JavaPierre HenryView Answer on Stackoverflow
Solution 4 - JavaOm PrakashView Answer on Stackoverflow
Solution 5 - JavaSumit SinghView Answer on Stackoverflow
Solution 6 - JavamadxView Answer on Stackoverflow
Solution 7 - JavaDaniel FerberView Answer on Stackoverflow
Solution 8 - JavaPiyush AgheraView Answer on Stackoverflow
Solution 9 - JavaEddy ZavalaView Answer on Stackoverflow
Solution 10 - JavaDENNIS KITHINJIView Answer on Stackoverflow
Solution 11 - JavaPadronização S AView Answer on Stackoverflow
Solution 12 - Javamourad kiratView Answer on Stackoverflow
Solution 13 - JavaSapView Answer on Stackoverflow