1

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);
      }
    } 
Ayaz Ali
  • 311
  • 2
  • 7
  • 16

2 Answers2

3

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);
      }
    }   
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • thanks for help and its not working with me may be i make mistake....i add simple dispose(); in JInternalFrame and the above code for jInternaleframe1 after that when press button 2 its close whole application and for button 1 also not working code... – Ayaz Ali Dec 11 '12 at 17:50
  • 1
    @AyazAli Im not sure please post an SSCCE but remember we only want to call dispose on the JInternalFrame instance and nothing else. Does copying and pasting my code for the one button work? – David Kroukamp Dec 11 '12 at 18:16
  • thanks for reply...i found my mistake and now code is working for me and this code is working for same jinternaleframe disposing only......now can you little more help me i need so that is, if jinternalframe 1 is open then i am going to open jinternalframe 2 so the jinternalfamre 1 is automatically close and only jinternalframe 2 open in jdesktopane... – Ayaz Ali Dec 11 '12 at 18:49
  • @AyazAli You'd do the same for the other button as this button and than in jinternalframe1 create button simply add call like in above code to dispose of jinternalframe1 – David Kroukamp Dec 11 '12 at 19:26
  • i did in both buttons after that the jinternalframe 1 and jinternalframe 2 repeat opening is stopped but when i clicked button 1 its open jinternalframe 1 and when i clicked button 2 its open jinternalframe 2 so the both ijinternalframes are open going to open in desktoppane and i want when i clicked one button 1 that open its open jinternalframe 1 and when i clicked on button 2 so its open jinternalframe 2 then auto close the jinternalframe 1...so how to do this ? – Ayaz Ali Dec 12 '12 at 05:28
  • @AyazAli close jinternalFrame1 in the method which creates jinternalFrame2. I cant help any further without a SSCCE as we are misunderstanding 1 another – David Kroukamp Dec 12 '12 at 06:52
1

Try this out.

Add this to your JButton's event:

JDesktopPane.removeAll();
JDesktopPane.updateUI();

//then add the JInternalFrame into the JDesktopPane

I know, I know, this question was 2 years ago, but hope this helps someone else :)