File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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="" target="_blank" rel="nofollow"></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

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

Joined: Mar 22, 2002
Posts: 7
Yeah - addWindowListener() with your anonymous inner class that calls a method updating the database, and then...
I agree. Here's the link:
subject: Update a database just before EXIT_ON_CLOSE
It's not a secret anymore!