How to set action for close button?

32.5k views Asked by At

I've created a stand-alone java desktop application in Netbeans 6.9. I want to set the action for the close button of my application. I want to know how and where to set the code for the action of that close button. Can anyone please help me regarding this?

6

There are 6 answers

1
RoflcoptrException On

You have to register an ActionListener on your close button. In this listener you can define what do to.

How add ActionListener to JButton in Java Swing

2
Petar Minchev On

Right-click on the button then, > Events > Action > actionPerformed. NetBeans will generate the action listener for you:)

Edit: If you want a close listener, then read here.

0
Maxim Welikobratov On

I think answers for How to close a java swing application from the code will be helpful too

0
trashgod On

Once you have the handler working, one convenient approach is to "set the default button by invoking the setDefaultButton() method on a top-level container's root pane." See the tutorial section How to Use JButton Features for details.

0
Claudio Nieder On

If you just want to exit the application when the close button is hit, you can use

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

as explained in the Oracle tutorial

0
Ordiel On

I dislike to wake up the zombies but, here is what I would do (this is considering that you are working in a Window based application, otherwise this is going to be useless!!!):

  1. I would set a window listener for the close operation here is the way to do it.
  2. Then I will delete manually the temp folders and files...

Obviously the window listener I'm talking about would go on the last window you (as a user) should close, this would not work either if you want it to happen when the last window is closed, but there is not an order specified to do that, some workarounds for that is making all your windows to share a flag/counter to indicate if it is time to delete the temp files/folders.

Again if your application is going to work underground sometimes (I mean that you can dispose all windows without shutting down the application), or is a daemon this won't work