JDesktopPane is a Swing container that allows us to create an MDI (Multiple Document Interface) application in Java. An MDI application is an application that can have multiple child windows within the main window. Each child window represents a different document or task, and the user can switch between them.
Following are the basic steps to use JDesktopPane in Java Swing:
- Create a JDesktopPane object and add it to our main JFrame or JApplet:
JDesktopPane desktopPane = new JDesktopPane(); add(desktopPane);
- Create a JInternalFrame object for each child window that we want to display. Set the size, location, and other properties of the JInternalFrame as desired.
JInternalFrame internalFrame = new JInternalFrame("Child Window", true, true, true, true); internalFrame.setSize(200, 200); internalFrame.setLocation(50, 50);
- Add any Swing components that we want to display in the child window to the JInternalFrame’s content pane.
JLabel label = new JLabel("Hello, world!"); internalFrame.getContentPane().add(label);
- Add the JInternalFrame to the JDesktopPane:
desktopPane.add(internalFrame);
- Finally, make the JInternalFrame visible by calling the setVisible() method:
internalFrame.setVisible(true);
The complete example:
import java.awt.BorderLayout;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
public class JDesktopPaneDemo extends JFrame {
public JDesktopPaneDemo() {
setTitle("JDesktopPane Demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
// Create a JDesktopPane and add it to the main JFrame
JDesktopPane desktopPane = new JDesktopPane();
add(desktopPane, BorderLayout.CENTER);
// Create a JInternalFrame and add it to the JDesktopPane
JInternalFrame internalFrame = new JInternalFrame("Child Window", true, true, true, true);
internalFrame.setSize(200, 200);
internalFrame.setLocation(50, 50);
desktopPane.add(internalFrame);
// Add a JLabel to the JInternalFrame's content pane
JLabel label = new JLabel("Hello, world!");
internalFrame.getContentPane().add(label);
// Make the JInternalFrame visible
internalFrame.setVisible(true);
}
public static void main(String[] args) {
JDesktopPaneDemo example = new JDesktopPaneDemo();
example.setVisible(true);
}
}
This code creates a JFrame with a JDesktopPane in the center. It then creates a JInternalFrame and adds it to the JDesktopPane. A JLabel is added to the JInternalFrame’s content pane and the JInternalFrame is made visible. When we run this code, we can see a small child window with the text “Hello, world!” in the center of the main window. See the below output of the above code:

With these steps, we are able to create a basic MDI application using JDesktopPane in Java Swing. Note that there are many additional features we can use with JDesktopPane, such as setting a background image or creating custom borders for the JInternalFrames.
Reference: https://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html