简体   繁体   中英

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

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... 在此输入图像描述

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

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

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM