开发者

OnExit Event For a Swing Application?

开发者 https://www.devze.com 2022-12-23 13:21 出处:网络
I\'m developing a simple application to manage the operatio开发者_如何学JAVAnal part of a business using Swing, but I need that when the application exits, it performs this:

I'm developing a simple application to manage the operatio开发者_如何学JAVAnal 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?


Runtime.getRuntime().addShutdownHook(new Thread()
{
    @Override
    public void run()
    {
        updateZonas();
        db.close();
    }
});

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


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);
        }
    });


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

0

精彩评论

暂无评论...
验证码 换一张
取 消