Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close a JavaFX application on window close?

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.

like image 327
Kshitiz Sharma Avatar asked Aug 28 '12 06:08

Kshitiz Sharma


People also ask

How do I exit JavaFX program?

stop() method and terminate the JavaFX application thread.

Which JavaFX application's lifecycle method the application shuts down?

You can terminate a JavaFX application explicitly using the methods Platform. exit() or System.

How do you stop a thread in JavaFX?

application. Application#stop() is called before the context terminates. Put inside the stop() method everything that needs to be executed before the JavaFX context terminates. With the System.


8 Answers

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

like image 138
Teocali Avatar answered Oct 06 '22 00:10

Teocali


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);
    }
});
like image 36
Cyrus13 Avatar answered Oct 06 '22 00:10

Cyrus13


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...
}
like image 32
Pierre Henry Avatar answered Oct 06 '22 01:10

Pierre Henry


stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        Platform.exit();
        System.exit(0);
    }
});
like image 31
Om Prakash Avatar answered Oct 06 '22 00:10

Om Prakash


Did you try this..setOnCloseRequest

setOnCloseRequest(EventHandler<WindowEvent> value)   

There is one example

like image 44
Sumit Singh Avatar answered Oct 06 '22 02:10

Sumit Singh


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
}
like image 38
Daniel Ferber Avatar answered Oct 06 '22 01:10

Daniel Ferber


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);
    });
}
like image 40
madx Avatar answered Oct 06 '22 01:10

madx


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();
    }
});
like image 34
Piyush Aghera Avatar answered Oct 06 '22 02:10

Piyush Aghera