Internal Frames

1. Internal Frame Basics

Many commercial Windows products such as Microsoft PowerPoint, Corel Draw, Symantec Visual Café, and Allaire HomeSite are Multiple Document Interface (MDI) applications. This means that there is one large "desktop" pane that holds all other windows. The other windows can be iconified (minimized) and moved around within this desktop pane, but not moved outside it. Furthermore, minimizing the desktop pane hides all the windows it contains as well. Swing introduced MDI support by means of two main classes. The first is JDesktopPane, and serves as a holder for the other windows. The second is JInternalFrame, which acts mostly like a JFrame, except that it is constrained to stay inside the JDesktopPane. Using the JInternalFrame constructor that just requires a title results in an internal frame that is not resizable, closeable, maximizable, or minimizable (i.e. iconifiable). However there is five-argument constructor that accepts boolean values for each of these properties (in that order). Internal frames also have two useful methods for controlling z-order: moveToFront and moveToBack.

2. Internal Frame Example

The following example creates five empty internal frames inside a desktop pane, which is in turn inside a JFrame. The screen shots below show the result with all frames open, and with two of the frames minimized.

3. JInternalFrames.java (Download source code)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class JInternalFrames extends JFrame {
  public static void main(String[] args) {
    new JInternalFrames();
  }

  public JInternalFrames() {
    super("Multiple Document Interface");
    WindowUtilities.setNativeLookAndFeel();
    addWindowListener(new ExitListener());
    Container content = getContentPane();
    content.setBackground(Color.white);
    JDesktopPane desktop = new JDesktopPane();
    desktop.setBackground(Color.white);
    content.add(desktop, BorderLayout.CENTER);
    setSize(450, 400);
    for(int i=0; i<5; i++) {
      JInternalFrame frame
        = new JInternalFrame(("Internal Frame " + i),
                             true, true, true, true);
      frame.setLocation(i*50+10, i*50+10);
      frame.setSize(200, 150);
      frame.setBackground(Color.white);
      desktop.add(frame);
      frame.moveToFront();
    }
    setVisible(true);
  }
}
Note: also requires WindowUtilities.java and ExitListener.java, shown earlier.

4. JInternalFrames: Initial Result

JInternalFrames

5. JInternalFrames: Result after Minimizing Two Internal Frames

JInternalFrames


This page is part of my Quick Swing Tutorial for AWT Programmers. © 1999 Marty Hall. All source code freely available for unrestricted use. Created for for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and Java short courses.