This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Update a database just before EXIT_ON_CLOSE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Update a database just before EXIT_ON_CLOSE" Watch "Update a database just before EXIT_ON_CLOSE" New topic
Author

Update a database just before EXIT_ON_CLOSE

ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
I have an application that uses a JFrame. I have also used jframe.setDefaultCloseOperation(EXIT_ON_CLOSE) to handle the closing of the JFrame using the windows "X" button available on most OS's.
However, what I want to happen is, if that "X" button on the title bar is click, i have to update a field in database with some additional information.
Right now that update only happens when the user uses the "EXIT" button that I have provided them.
Is there a way to call some method when the "X" on the windows title bar is clicked in order to make this update before the application closes?
I suppose I could just not enable that function but that doesn't make for a very flexible solution and end users will hate that that have to use the EXIT button that I created.


Ryan Headley<br /><a href="http://www.sudovi.com" target="_blank" rel="nofollow">http://www.sudovi.com</a>
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Ryan,
Try adding a WindowListener to your JFrame instead. You can extend the WindowAdapter class and put your code in the (overridden) windowClosing() method followed by a System.exit(0).
Hope this helps
Michael Morris
SCJP


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
ryan headley
Ranch Hand

Joined: Jun 28, 2000
Posts: 156
That should do it...should I make that and anonymous inner class like ActionListener?
Adam Blinkinsop
Greenhorn

Joined: Mar 22, 2002
Posts: 7
Yeah - addWindowListener() with your anonymous inner class that calls a method updating the database, and then...
 
GeeCON Prague 2014
 
subject: Update a database just before EXIT_ON_CLOSE