views:

83

answers:

4

I have the main thread from which I start a window using invokeLater. I run my application from command line. So, when application is running I see the window and my command line is "blocked" by the application.

I can stop the application either by closing the window (as a result the command line is unblocked) or by typing Ctrl-C in the command line (as a result the window disappear).

I wanted to be able to stop the application by clicking on a button in the window of the application. I used setVisible(false) for that. But in this way I can achieve the goal only partially. My window really disappear but the command line is still blocked. So, the software is still running.

Well, I assume it's because some other threads are still running. But how can I easily close all these threads (like I do by closing the window of the application manually).

+3  A: 
System.exit(0);
Eric Eijkelenboom
"Clicking the button" will trigger an event. Put this code in the event handler.
Dave
Thanks! It works for me.
Roman
Keep in mind that this is a rather harsh way to kill your application.
PartlyCloudy
You should award the answer, then :) @PC: True, but no different than CTRL-C, which is the current method.
Dave
On every paragraph I read on the original post my mind was thinking `System.exit, System.exit` once I've finish to read the question, I see this as the first answer and turns out to be what I thought :) +1
OscarRyz
Dave, I will award. Stackoverflow writes me that I can do it in 2 minutes. :) When I tried to do it for the first time, it wrote me that I need to wait for 9 minutes.
Roman
A: 

You must finish all threads in order to stop your application. Just hiding the GUI will not finish the AWT-Thread. Have a look at the API of the GUI classes you use like the dispose-methods.

PartlyCloudy
A: 

Try Window.dispose()

Alinium
+1  A: 

If it's a JFrame you're showing, you can tell it to exit the app when the frame is closed - the default is to just hide the frame:

 myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

This will exit the app if the user closes the window (top right [x] button often) , in addition you could have a Quit button whose event handler closes the window using myFrame.dispose();

nos