tags:

views:

154

answers:

3

I'm developing a simple application to manage the operational part of a business using Swing, but I need that when the application exits, it performs this:

updateZonas();
db.close();

But how can I do this?

+3  A: 

Add a WindowListener to your JFrame. Its windowClosing method would call whatever code you need, then System.exit(0) (or some other return code).

lins314159
+4  A: 

Are you using a JFrame? if so you can try this:

    myframe.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosing(WindowEvent winEvt) {
            updateZonas();
            db.close();
            System.exit(0);
        }
    });
Enrique
Also put a try/catch/finally around the updateZonas and dbclose calls in case something is wrong. You can let the user know something went wrong and decide whether or not the app should still exit.
Jeff Storey
+4  A: 
Runtime.getRuntime().addShutdownHook(new Thread()
{
    @Override
    public void run()
    {
        updateZonas();
        db.close();
    }
});

This works for any Java application(Swing/AWT/Console)

Santhosh Kumar T