Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Update a database just before EXIT_ON_CLOSE

 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should do it...should I make that and anonymous inner class like ActionListener?
 
Adam Blinkinsop
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah - addWindowListener() with your anonymous inner class that calls a method updating the database, and then...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic