2

Is there a way to add/override the action event of THIS button ? I couldn't find anywhere how to acces this button and override it's action. In my case I need to do this because I need to save the resources before I exit my window, and if I press the x button, It will exit automatically.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Teo
  • 3,394
  • 11
  • 43
  • 73
  • 1
    Uh...we definitely need more information. What kind of component is the button? A Swing component? – mre Mar 06 '12 at 20:59

3 Answers3

4

On the JFrame:

setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

Then add a WindowsListener and in the windowClosing(WindowEvent event) methods do your job and then call dispose()

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
3

I'd use a shut-down hook instead that way it'll save even if you close the app via another method. Looky here

Community
  • 1
  • 1
Inverted Llama
  • 1,522
  • 3
  • 14
  • 25
2

You can try using a WindowListener interface. If I remember correctly, it should allow you to do things when something on the frame is clicked on(like exiting).

See http://docs.oracle.com/javase/6/docs/api/java/awt/event/WindowListener.html

mooles
  • 311
  • 2
  • 5
  • 11