Monday, January 27, 2014

How to execute code when JInternalFrame is closing programmatically

To programmatically close a JInternalFrame, the JInternalFrame setClosed is programmactically called to set the IS_CLOSED_PROPERTY to true. It calls the fireInternalFrameEvent, InternalFrameListener.internalFrameClosing, dispose and InternalFrameListener.internalFrameClosed methods in the order listed if there is an InternalFrameListener added to the JInternalFrame, and then set the IS_CLOSED_PROPERTY to trueIf there is no InternalFrameListener added to the JInternalFrame, the InternalFrameListener methods will not be called. The JInternalFrame dispose method is called and the internal frame is closed.  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 setClosed. 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 setClosed (boolean closed) throws java.beans.PropertyVetoException {
        System.out.println("setClosed....");
        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.setClosed(closed);
    }

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

                //programmatically close the internal frame

                try {
                    test.setClosed(true);
                } catch (java.beans.PropertyVetoException e) {
                    System.out.println(e.getMessage());
                }
            }
        });
    }
}
        
-----------------------------------------------------------------------------------------------------------------
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 JFrame is closing after the [X] button is pressed?

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

2 comments:

  1. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery

    ReplyDelete
  2. Great Article android based projects

    Java Training in Chennai

    Project Center in Chennai

    Java Training in Chennai

    projects for cse

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

    ReplyDelete