Search code examples
javaparent-childjinternalframe

How dispose a child from JInternalFrame parent?


i have a parent class and a child class (both JInternalFrame), my child is necessary if my parent is open. I search and search and nothing... Just vague answers. How i can dispose a child closing a parent? (yeah i know i'm saying child instead children but child just can open once, so yeah is child) I tried with instance but i'm not so good with that. A big thanks. =)

Link: https://pastebin.com/5MLUZDBZ


Solution

  • For next questions, it is recommended that you provide a Minimal, Complete, and Verifiable example.

    However looking at your code, you just have to make it so that each Parentinternal frame keeps track of its own Child internal frames (see the List called children).

    Then when you want to dispose the parent, dispose each child from the list first.

    Here is the modified version of your Parent class :

    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.JDesktopPane;
    
    public class Parent extends javax.swing.JInternalFrame {
    
        private final JDesktopPane pane;
        private final List<Child> children = new ArrayList<>();
    
        public Parent(final JDesktopPane pane) {
            this.pane = pane;
            initComponents();
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
    
            jButton1.setText("Open child");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(final java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("Close all");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(final java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(jButton1)
                                    .addGap(37, 37, 37)
                                    .addComponent(jButton2)
                                    .addContainerGap(193, Short.MAX_VALUE)));
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                    .addGap(30, 30, 30)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(jButton1)
                                            .addComponent(jButton2))
                                    .addContainerGap(225, Short.MAX_VALUE)));
    
            pack();
    
        }// </editor-fold>
    
        private void jButton1ActionPerformed(final java.awt.event.ActionEvent evt) {
            Child c = new Child();
            children.add(c);
            pane.add(c);
            c.setVisible(true);
            c.toFront();
        }
    
        private void jButton2ActionPerformed(final java.awt.event.ActionEvent evt) {
    
            //CLOSE THIS WINDOW AND CHILD
            for (Child child : children) {
    
                child.dispose();
            }
    
            this.dispose();
    
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        // End of variables declaration
    }