How to close previous jInternalFrame when open new jInternalFrame in the jDesktopPane

Ayaz Ali picture Ayaz Ali · Dec 11, 2012 · Viewed 11.6k times · Source

I created MDI (Multiple Document interface) in java using netbeans in which i have two jbuttons and one jdesktoppane so when clicked on both button then both jinternalframes are opened in same jdesktoppane so i want how to close previous jinternalframe when open new jinternalframe in the jdesktoppane ?

Check snapshot for more better understand my question what i want... enter image description here

First jButton code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      try
      {

       tst t = new tst();
        JInternalFrame internalFrame1 = new JInternalFrame("Test Window1"); 
        internalFrame1.add(t.getContentPane());
        internalFrame1.pack();

        internalFrame1.setVisible(true);
        q.add(internalFrame1);

        internalFrame1.setClosable(true);  

        BasicInternalFrameUI ui = (BasicInternalFrameUI)internalFrame1.getUI();
        Container north = (Container)ui.getNorthPane();
        north.remove(0);
        north.validate();
        north.repaint();

        for(MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame1.getUI()).getNorthPane().getMouseListeners()){
        ((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame1.getUI()).getNorthPane().removeMouseListener(listener);
        }

         internalFrame1.setSelected(true);

      }
      catch(Exception ex)
      {
          JOptionPane.showMessageDialog(null, ex);
      }
    }   

Second button Code:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try
        {

        zxy z = new zxy();
        JInternalFrame internalFrame = new JInternalFrame("Test Window2"); 
        internalFrame.add(z.getContentPane());
        internalFrame.pack();
        internalFrame.setSize(570,420);

        internalFrame.setVisible(true);
        q.add(internalFrame);

        internalFrame.setClosable(true);  

        BasicInternalFrameUI ui = (BasicInternalFrameUI)internalFrame.getUI();
        Container north = (Container)ui.getNorthPane();
        north.remove(0);
        north.validate();
        north.repaint();

        for(MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame.getUI()).getNorthPane().getMouseListeners()){
        ((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame.getUI()).getNorthPane().removeMouseListener(listener);
        }

         internalFrame.setSelected(true);

        }
      catch(Exception ex)
      {
          JOptionPane.showMessageDialog(null, ex);
      }
    } 

Answer

David Kroukamp picture David Kroukamp · Dec 11, 2012

Simply call dispose() on JInternalFrame instance.

To do this you will need to move your JInternalFrame declaration out of the method so that we may check if the instance is not null (thus there is an existing instance and than call dispose() on the instance before creating a new one:

JInternalFrame internalFrame1;

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      try
      {

       if(internalFrame1 !=null) {//make sure its not null
           internalFrame1.dispose();//close the previos internalframe
       }

        tst t = new tst();
        internalFrame1 = new JInternalFrame("Test Window1"); //create new instance of internal frame
        internalFrame1.add(t.getContentPane());
        internalFrame1.pack();

        internalFrame1.setVisible(true);
        q.add(internalFrame1);

        internalFrame1.setClosable(true);  

        BasicInternalFrameUI ui = (BasicInternalFrameUI)internalFrame1.getUI();
        Container north = (Container)ui.getNorthPane();
        north.remove(0);
        north.validate();
        north.repaint();

        for(MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame1.getUI()).getNorthPane().getMouseListeners()){
        ((javax.swing.plaf.basic.BasicInternalFrameUI) internalFrame1.getUI()).getNorthPane().removeMouseListener(listener);
        }

         internalFrame1.setSelected(true);

      }
      catch(Exception ex)
      {
          JOptionPane.showMessageDialog(null, ex);
      }
    }