Externalizing a JInternalFrame in a JFrame

You can get the contentpane of a JInternalFrame and put it inside a JFrame. Check out the following example, specifically the classes MyJInternalFrame and MyJFrame.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.util.*;
import java.awt.*;
import java.net.*;
 
public class Main extends JFrame {
   JDesktopPane desktop;
   int nframes = 0;
  
   public Main() {
      desktop = new JDesktopPane(); 
      setContentPane(desktop);
      setJMenuBar(createMenuBar());
      createInternalFrame(); 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   }
 
   protected JMenuBar createMenuBar() {
      JMenuBar menuBar = new JMenuBar();
 
      JMenu createMenu = new JMenu("Create");
      createMenu.setMnemonic(KeyEvent.VK_C);
      JMenuItem newMenuItem = new JMenuItem("New");
      newMenuItem.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            createInternalFrame();
         }
      }); 
      newMenuItem.setMnemonic(KeyEvent.VK_N);
      createMenu.add(newMenuItem);
      menuBar.add(createMenu);
 
      return menuBar;
   }
 
   protected void createInternalFrame() {
      nframes++;
      String title = "JInternalFrame #" + nframes;
      MyJInternalFrame frame = new MyJInternalFrame(title,
         true,    // resizable
         true,    // closable
         true,    // maximizable
         true);   // iconifiable
      frame.setVisible(true); 
 
      desktop.add(frame);
      frame.setSize(200, 200);
      frame.setLocation(30*nframes, 30*nframes);
      try {
         frame.setSelected(true);
      } catch (java.beans.PropertyVetoException e) {}
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(500, 300);
      main.setVisible(true);
   }
} 
 
class MyJInternalFrame extends JInternalFrame 
                       implements VetoableChangeListener { 
   MyJFrame externalFrame = null;
 
   public MyJInternalFrame(String title, boolean resizable, 
                           boolean closable, boolean maximizable, 
                           boolean iconifiable) { 
      super(title, resizable, closable, maximizable, iconifiable); 
      addVetoableChangeListener(this); 
 
      getContentPane().setLayout(new GridLayout(3, 1));
      getContentPane().add(new JLabel("JLabel"));
      getContentPane().add(new JButton("JButton"));
      getContentPane().add(new JCheckBox("JCheckBox"));
   } 
 
   public void vetoableChange(PropertyChangeEvent pce) 
                                 throws PropertyVetoException { 
      if (pce.getPropertyName().equals(IS_CLOSED_PROPERTY)) { 
         boolean changed = ((Boolean) pce.getNewValue()).booleanValue(); 
         if (changed) { 
            setVisible(false);
            boolean packFrame = false;
            if (externalFrame == null) {
               externalFrame = new MyJFrame(this.getTitle(), this);
               packFrame = true;
            }
            Container cp = this.getContentPane();
            this.getRootPane().setContentPane(new JPanel());
            externalFrame.getRootPane().setContentPane(cp);
            externalFrame.setVisible(true);
 
            // remember frame size if frame was already shown before
            if (packFrame) externalFrame.pack();
             
            throw new PropertyVetoException("Intercepted Closing",null); 
         } 
      } 
   }
} 
 
class MyJFrame extends JFrame
{
   MyJInternalFrame internalFrame;
 
   public MyJFrame(String title, MyJInternalFrame internalFrame) {
      super(title);
      this.internalFrame = internalFrame;
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) { 
            MyJFrame frame = MyJFrame.this;
            frame.setVisible(false);
            Container cp = frame.getContentPane();
            frame.getRootPane().setContentPane(new JPanel());
            frame.internalFrame.getRootPane().setContentPane(cp);
            frame.internalFrame.setVisible(true);
         } 
      });
   }
}