aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Closing program when GUI is closed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Closing program when GUI is closed" Watch "Closing program when GUI is closed" New topic
Author

Closing program when GUI is closed

Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Hi,

I have one JFrame running in my program, and some non-gui threads. I want to perform a shutdown routine on the threads when the JFrame is closed. To do this, I have set JFrame.DISPOSE_ON_CLOSE. In another thread, I call join() on the AWT thread, so that when the AWT thread finishes (JFrame dispose()) I can excute the shutdown routines:


It seems to work. Is this a right way of doing things?

Thanks.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Depends on how insulated that code is against other classes being in the mix. All it takes is just one running Swing Timer to keep the EDT alive.

edit Additionally, setDefaultCloseOperation(...) isn't a static method.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Mikpo Siru wrote:I want to perform a shutdown routine on the threads when the JFrame is closed.

Use a WindowListener and its windowClosed method. Don't be confused with windowClosing - that's called when you press the X button to close the frame. windowClosed will be called when the window is actually closed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mikpo Siru
Ranch Hand

Joined: Apr 09, 2009
Posts: 54
Thanks for your suggestions.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Closing program when GUI is closed