Monday, January 27, 2014

How to execute code when a JInternalFrame is closing after the [X] button is pressed

When the [X] button of JInternalFrame is clicked, it calls the doDefaultClosingAction first, then the fireInternalFrameEvent, InternalFrameListener.internalFrameClosing, dispose and InternalFrameListener.internalFrameClosed methods in the order listed if there is a InternalFrameListener added to the JInternalFrame. If there is no InternalFrameListener added to the JInternalFrame, the InternalFrameListener methods as well as the JInternalFrame dispose method will not be called and the internal frame will not close when the [X] button is clicked. If you would like to let the user to decide if he/she really wants to close the internal frame, the method to override is doDefaultClosingAction. If you just want to do some setting/cleaning , you may override any of these methods.

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class InternalFrameTest extends JInternalFrame {

    public InternalFrameTest() {
        setClosable(true);
        setSize(250, 150);
        add(new JLabel("Internal Frame Test"));
     
        setVisible(true);
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
            System.out.println(e.getMessage());
        }
     
        this.addInternalFrameListener(new InternalFrameAdapter() {
            @Override
            public void internalFrameClosing(InternalFrameEvent e) {
                System.out.println("internalFrameClosing....");
                //do some cleaning here
                super.internalFrameClosing(e);
            }
         
            @Override
            public void internalFrameClosed(InternalFrameEvent e) {
                System.out.println("internalFrameClosed....");
                //do some cleaning here
                super.internalFrameClosed(e);
            }
        });
    }

    @Override
    protected void fireInternalFrameEvent(int id) {
        if (id == InternalFrameEvent.INTERNAL_FRAME_CLOSING) {
            System.out.println("fireInternalFrameEvent....");
            //do some cleaning here
        }
        super.fireInternalFrameEvent(id);
    }
 
    @Override
    public void doDefaultCloseAction() {
        System.out.println("doDefaultCloseAction....");
        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;
        }
        //do some cleaning here
        super.doDefaultCloseAction();
    }

    @Override
    public void dispose() {
        System.out.println("dispose...");
        super.dispose();
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                JDesktopPane dtp = new JDesktopPane();
                frame.setContentPane(dtp);
                InternalFrameTest test = new InternalFrameTest();
                dtp.add(test);

                frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                frame.setSize(300, 200);
                frame.setVisible(true);
            }
        });
    }
}
 
-----------------------------------------------------------------------------------------------------------------
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.
-----------------------------------------------------------------------------------------------------------------------------

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

No comments:

Post a Comment