• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exiting a multi-threaded Swing application without System.exit

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you need to do it after it's started?
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic