Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exiting a multi-threaded Swing application without System.exit  RSS feed

 
Levente Szekrenyes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a problem with this Swing application we develop.It uses a lot of threads running in the background, and when the user closes it, the application graciously calls System.exit. However, this applications needs to be called from within another one (for testing purposes), and when calling System.exit, the "parent" application also dies.

So my question is: how can I close the application, without calling System.exit?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Providing you're using a reasonably recent Java (1.4 on, I think), you can exit a Swing application without doing System.exit(). You need to ensure that your JFrames etc. all have a parent that is under your control, and never the hidden parent that Swing instantiates when no parent is specified. You can create a dummy parent, which you never show on-screen, purely to be the parent of your top-level frame. When you want to exit the application, you close/dispose all the frames, including the dummy parent.
 
Levente Szekrenyes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is, I have several background threads running, which don't stop just because I'm disposing the main window, so my application isn't "exiting", it just doesn't show anything on screen.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If those other treads should exit once the main GUI has gone away, you can make the daemon threads (check the Thread class for which method to use).
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or have them check a condition and modify that condition so that those threads die. By the sounds of it they should be daemon threads.
 
Levente Szekrenyes
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answers. It seems like there is no easy way Gonna need to rewrite a few classes.

Too bad a thread cannot be set to daemon after it was started.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to do it after it's started?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!