Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my application still run after closing main window?

Tags:

java

swing

If I make a JFrame like this

public static void main(String[] args) {

     new JFrame().setVisible(true);
}

then after closing the window the appication doesn't stop (I need to kill it).

What is the proper way of showing application's main windows ?

I'd also like to know a reason of a proposed solution.

Thanks in advance.

like image 283
Łukasz Bownik Avatar asked Oct 29 '08 09:10

Łukasz Bownik


People also ask

Why is an app still running?

The normal routine for most mobile phone apps includes running in the background. That constant activity keeps them functional for when you need them and helps other apps function as well.

Why do some programs not close?

Seriously, when a program refuses to close, this usually indicates that the program is performing a critical function and closing could cause corruption in the program. This can happen even though the system may view the program as being in a non-responding state.


2 Answers

You should call the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); in your JFrame.

Example code:

public static void main(String[] args) {
    Runnable guiCreator = new Runnable() {
        public void run() {
            JFrame fenster = new JFrame("Hallo Welt mit Swing");
            fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            fenster.setVisible(true);
        }
    };
    SwingUtilities.invokeLater(guiCreator);
}
like image 85
Burkhard Avatar answered Oct 11 '22 13:10

Burkhard


There's a difference between the application window and the application itself... The window runs in its own thread, and finishing main() will not end the application if other threads are still active. When closing the window you should also make sure to close the application, possibly by calling System.exit(0);

Yuval =8-)

like image 23
Yuval Avatar answered Oct 11 '22 15:10

Yuval