Monday, January 27, 2014

How to execute code when JFrame is closing after the [X] button is pressed?

The dispose() method is only called when the default close operation is set to JFrame.DISPOSE_ON_CLOSE (setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE)). When the default close operation is set to JFrame.EXIT_ON_CLOSE, the JVM and everything else is shutdown and there is no need to dispose. When it is set to DO_NOTHING_ON_CLOSE or HIDE_ON_CLOSE, it means that you do not want the dispose() to be called and indeed it is not called. If you do not specifically set the default close operation value, by default it is HIDE_ON_CLOSE.

Therefore, overriding the dispose() method does not guarantee that your code is executed. The correct way to do this is either to add a WindowListener to the JFrame or override the processWindowEvent method. If you would like the user to decide if he/she truly wants to close the frame, the method to override is processWindowEvent. If you just want to do some setting/cleaning, you may override either of these methods.

1. Add a WindowListener to the JFrame


import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class WindowCloseTest extends JFrame {
    public WindowCloseTest() {
        this.add(new JLabel("WindowCloseTest"));
        setSize(300, 200);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e){
                //your code to be executed before window is closed.
                //not the place to opt out closing window
                System.out.println("exiting1...");
                super.windowClosing(e);
                System.out.println("exiting1.1...");
            }
        });
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                WindowCloseTest test = new WindowCloseTest();
                test.setVisible(true);
            }
        });
    }
}

2. Override the processWindowEvent method


import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class WindowCloseTest extends JFrame {
    public WindowCloseTest() {
        this.add(new JLabel("WindowCloseTest"));
        setSize(300, 200);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    @Override
    protected void processWindowEvent(WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            int opt = JOptionPane.showConfirmDialog(null,
                    "Are you sure to close this window?", "Confirm",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
            if (opt == JOptionPane.NO_OPTION) {
                return;
            }
            System.out.println("exiting2.2...");
            //your code to be executed before window is closed.
        }
       
        super.processWindowEvent(e);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                WindowCloseTest test = new WindowCloseTest();
                test.setVisible(true);
            }
        });
    }
}

References:
1. How to execute code when JInternalFrame is closing programmatically
2. How to execute code when a JInternalFrame is closing after the [X] button is pressed

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

No comments:

Post a Comment