JDesktopPane Example Java Swing

JDesktopPane Example Java Swing

JDesktopPane – Multiple Document Interface (MDI)

Many of today’s applications use a multiple document interface (MDI) [i.e., a main window (often called the parent window) containing other windows (often called child windows)] to manage several open documents that are being processed in parallel. Many commercial Window 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. For example, many email programs allow you to have several email windows open at the same time so that you can compose and/or read multiple email messages. Similarly, many word processors allow(MS-Word) to open multiple documents in separate windows so the user can switch between the documents without having to close the current document to open another document.

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.

The following program demonstrates Swing’s JDesktopPane and JInternalFrame classes. JInternalFrame class provides support for creating multiple document interfaces(windows) that can be added to JDesktopPane. The five child windows created simply display a message "S. Nageswara Rao, Sr.Faculty".

JDesktopPane is an extension of the JLayeredPane class and maintains an association with the DesktopManager interface. Each instance of JDesktopPane has a desktop manager that is responsible for implementing look-and-feel behavior. JDesktopPane can also be extended to provide customized behavior. For example, a common feature of Multiple Document Interface (MDI) applications is the ability to cascade the frames contained in the desktop pane.

Instance of JDesktopPane are constructed with the JDesktopPane no argument constructor, which is the only constructor JDesktopPane provides. Instances of JInternalFrame are subsequently added to the desktop pane.

Following program adds five detachable (drag by holding any internal frame) JInternalFrames to JDesktopPane.


CustomDesktopPane desktopPane = new CustomDesktopPane();

CustomDesktopPane is a class customized that extends JDesktopPane.


desktopPane is an object of CustomDesktopPane. With this object, display() method is called and also passed as parameter. This is the way, how we can make objects to communicate between multiple classes.

JInternalFrame jif = new JInternalFrame(” Internal Frame ” + i , true , true , true , true);

The JinternalFrame constructor used here requires five arguments — a String for the title bar of the internal window, a boolean indicating if the internal frame should be resizable by the user, a boolean indicating if the internal frame should be closable by the user, a boolean indicating if the internal frame should be maximizable by the user and a boolean indicating if the internal frame should be minimizable by the user. For each of the boolean arguments a true value indicates that the operation should be allowed.

jif.setBounds(x , y , 250 , 85);

A component’s size is decided by the layout manager. We can also set the size of a component by ourselves using setBounds() method. The first two parameters indicates the coordinates(x and y) where the component is to be displayed in the container. The last two parameters give the size of the component in terms of width and height.

Container c1 = jif.getContentPane();

As with JFrame and JApplet, a JInternalFrame has a content pane to which GUI components can be attached. The above statement gets a reference to the JInternalFrame’s content pane.


Each internal frame is added to the JDesktopPane( extended by our class CustomDesktopPane).

y += 85;

y is incremented by 85 pixels so that one frame can be displayed below another.

Leave a Reply

Your email address will not be published. Required fields are marked *