Java Swing Tutorial - Java Swing MDI

MDI stands for Multiple Document Interface. In an MDI application, one main window is opened, and multiple child windows are open within the main window.

In an MDI application, we can open multiple frames that will be instances of the JInternalFrame class.

We can organize multiple internal frames in many ways. For example, we can maximize and minimize them; we can view them side by side in a tiled fashion, or we can view them in a cascaded form.

The following are four classes we will be working with in an MDI application:

  • JInternalFrame
  • JDesktopPane
  • DesktopManager
  • JFrame

An instance of the JInternalFrame class acts as a child window that is displayed inside the area of its parent window.

We can add Swing components to JInternalFrame content pane, pack them using the pack() method, and make it visible using the setVisible(true) method.

To listen to window events such as activated, deactivated, etc., we need to add an InternalFrameListener to the JInternalFrame instead of a WindowListener, which is used for a JFrame.

The following code shows how to use an instance of the JInternalFrame class:

String title = "A Child   Window"; 
Boolean  resizable = true;
Boolean  closable = true; 
Boolean  maximizable = true; 
Boolean  iconifiable = true; 
JInternalFrame iFrame = new JInternalFrame(title, resizable, closable, maximizable, iconifiable);

Add components to the iFrame using


Pack eth frame and make it visible


JDesktopPane is used as a container for all JInternalFrame. It uses a null layout manager.

JDesktopPane   desktopPane = new JDesktopPane();
// Add all  JInternalFrames to the   desktopPane 

We can get all JInternalFrames that are added to a JDesktopPane using its getAllFrames() method.

JInternalFrame[] frames   = desktopPane.getAllFrames();

A JDesktopPane uses an instance of the DesktopManager interface to manage all internal frames.

The DefaultDesktopManager implements DesktopManager interface.

The desktop manager has many useful methods. For example, to close an internal frame programmatically, use its closeFrame() method.


The following code demonstrates how to develop an MDI application.

import java.awt.BorderLayout;
import java.awt.Dimension;
/*from  w w  w. j av a2 s. co  m*/
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Main extends JFrame {
  private final JDesktopPane desktopPane = new JDesktopPane();

  public Main() {
    JInternalFrame frame1 = new JInternalFrame("Frame 1", true, true, true,

    JInternalFrame frame2 = new JInternalFrame("Frame 2", true, true, true,

    frame1.getContentPane().add(new JLabel("Frame 1  contents..."));

    frame2.getContentPane().add(new JLabel("Frame 2  contents..."));

    int x2 = frame1.getX() + frame1.getWidth() + 10;
    int y2 = frame1.getY();
    frame2.setLocation(x2, y2);


    this.add(desktopPane, BorderLayout.CENTER);

    this.setMinimumSize(new Dimension(300, 300));
  public static void main(String[] args) {
    try {
    } catch (Exception e) {
    SwingUtilities.invokeLater(() -> {
      Main frame = new Main();