1. JDesktopPane and JInternalFrame
In Java Swing, one can create MDI (Multiple Document Interface) applications using JDesktopPane and JInternalFrame. The JDesktopPane is the parent for JInternalFrame, and it can manage multiple internal frames. For this reason, Java Swing extends JDesktopPane from JLayeredPane to know how to manage the overlapping internal frames. JInternalPane provides the functionality of the JFrame, like resize, move, minimize, and maximize. But it will work for the parent JDesktopPane. One can add swing components to the JInternalFrame and finally give it to the JDesktopPane.
2. About the JDesktopPane Example
The below screen shows the JDesktopPane Example:

In this example, we will derive from a JInternalFrame to display html content via the JEditorPane. In the bottom, we have JTextField where you can set a title for the JInternalFrame. When you click Show Html, a static html content will be displayed in the JInternalFrame. You can open multiple JInternalFrame. The example shows static html content, but you can extend this example to ask the user to select a Html file from the disc.
3. Prepare Bottom Panel
The bottom of JFrame contains a JPanel which holds a JLabel, JTextField and a JButton. Snippet-01 sets size and position for the JFrame. In Snippet-02, we created the JTextField and
JDesktopPane. The variables lx
and ly
decide the location of the JInternalFrame. Snippet-3 assembles all these controls inside the JPanel before adding it to the JFrame’s bottom area.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//Sample 02: Members JTextField jt = new JTextField(25); JDesktopPane DTPane = new JDesktopPane(); int lx = 0; int ly = 0; @SuppressWarnings("unchecked") public SwingInternalPane(String title) { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 750, 600); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Run 1. //Sample 03: Create South Panel JPanel cmdPanel = new JPanel(); JButton btnShowHtml = new JButton("Show Html"); JLabel lbl = new JLabel("Document Title"); cmdPanel.add(lbl); cmdPanel.add(jt); cmdPanel.add(btnShowHtml); add(cmdPanel, BorderLayout.SOUTH); |
4. Equip JDesktopPane on JFrame
In section 3, we made the bottom panel ready and displayed it. Now, we add the JDesktopPane to the centre part of the JFrame window via the add
method call. Before adding, we set a Gray background color for the desktop pane via the method call
setBackground.
1 2 3 |
//Sample 04: Prepare Desktop Pane & add DTPane.setBackground(Color.LIGHT_GRAY); add(DTPane); |
5. Html Document as JInternalFrame
JDesktopPane can host multiple JInternalFrame in it. In the below code, we derive HtmlWindow from the JInternalFrame. Our constructor takes a string to set the title for the internal frame.

Code Explanation
HtmlWindow
Derives from the JInternalFrame so that it can be added to the JDesktopPane.- The snippet creates the
JEditorPane
to show html content in it. - This snippet creates a
JScrollPane
to manage the content of theJEditorPane
. After setting the preferred size for the pane, the code claims to have vertical scrollbar. Finally, we add the JScrollPane, which is managing the JEditorPane, to the JInternalFrame via the add method call. - Here, we get the system path to the html file in
URL
format. - We set the html page to the JEditorPane via the function call
setPage
.
Note, the JInternalFrame we are preparing here will be housed in the JDesktopPane. The container-child relations go like this:
- JFrame Window manages the JDesktopPane in its centre part by employing the BorderLayout manager.
- JDesktopPane will manage one or more JInternalFrame.
- JInternalPane houses JScrollPane.
- JScrollPane is housing the JEditorPane and provides scrolling support.
- JEditorPane is the one which takes care of displaying the document. Here in our case, it is a Html Document.
This way, our example is an MDI Application for Html Document Display.
6. Display JInternalFrame in JDecktopPane (MDI)
We display the JInternalFrame when user clicks the Show Html button. Below is the handler code and its explanation:

Code Explanation
- Each time when user click the Show Html button, we should show JInternalFrame which displays the html content. We change the X, Y location so that we can see the JInternalFrame stacked over the previous one.
- In this snippet, we construct the JInternalFrame derived instance
HtmlWindow
. We pass the title for the JInternalFrame from the JTextField which presents in the JFrame. Once the object is ready, this snippet adds the internal frame to the JDesktopPane by calling theadd
method. - The
setClosable method presents an ‘X’ button on the frame title so that the user can close internal frame. We display the JInternalFrame at the calculated location
lx
andly
. Thepack
method will ask the layout manager to pack all the components in the JInternalFrame and, if needed, resize it. - Here, setSizable method allows the user to resize the JInternalFrame. The methods setMaximizable and setIconifiable displays the Minimize and maximize button in the top right corner of the JInternalFrame.
That is all! We are done with the example. You can watch the video in the next section and code snippets are also provided for copy & paste.
7. Youtube Demo – JDesktopPane
8. Code Reference
8.1 MainEntry.java
1 2 3 4 5 6 7 8 9 |
package tube.coding.examples; public class MainEntry { public static void main(String[] args) { SwingInternalPane frame = new SwingInternalPane("Swing Internal Pane Example"); frame.setVisible(true); } } |
8.2 HtmlWindow.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.io.IOException; import java.net.URL; import javax.swing.JEditorPane; import javax.swing.JInternalFrame; import javax.swing.JScrollPane; //Sample 05: Create Window to show html public class HtmlWindow extends JInternalFrame { public HtmlWindow(String title) { //Sample 06: Set Size and Position super(title); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Sample 07: Create Editor Pane to show html JEditorPane jep = new JEditorPane(); jep.setEnabled(true); JScrollPane scroll = new JScrollPane(jep); Dimension ScrollSize = new Dimension(500, 150); scroll.setPreferredSize(ScrollSize); scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); ControlHost.add(scroll); URL HtmlPage = HtmlWindow.class.getResource("Test.html"); try { jep.setPage(HtmlPage); } catch (IOException e) { e.printStackTrace(); } } } |
8.3 SwingInternalPane.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
package tube.coding.examples; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.URL; import javax.swing.JButton; import javax.swing.JDesktopPane; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; @SuppressWarnings("serial") public class SwingInternalPane extends JFrame { //Sample 02: Members JTextField jt = new JTextField(25); JDesktopPane DTPane = new JDesktopPane(); int lx = 0; int ly = 0; @SuppressWarnings("unchecked") public SwingInternalPane(String title) { super(title); //Sample 01: Set Size and Position setBounds(100, 100, 750, 600); Container ControlHost = getContentPane(); ControlHost.setLayout(new BorderLayout()); //Run 1. //Sample 03: Create South Panel JPanel cmdPanel = new JPanel(); JButton btnShowHtml = new JButton("Show Html"); JLabel lbl = new JLabel("Document Title"); cmdPanel.add(lbl); cmdPanel.add(jt); cmdPanel.add(btnShowHtml); add(cmdPanel, BorderLayout.SOUTH); //Run2. //Sample 04: Prepare Desktop Pane & add DTPane.setBackground(Color.LIGHT_GRAY); add(DTPane); //Run 3. //Sample 08: Show Html Document btnShowHtml.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //8.1 Calculate the location lx = lx + 10; ly = ly + 10; //8.2 Create Internal pane and add it to Document Pane HtmlWindow HtmlInternalWindow = new HtmlWindow(jt.getText()); DTPane.add(HtmlInternalWindow); //8.3 Display the Internal Pane HtmlInternalWindow.setClosable(true); HtmlInternalWindow.setVisible(true); HtmlInternalWindow.setLocation(lx, ly); HtmlInternalWindow.pack(); //8.4 Check Other Properties HtmlInternalWindow.setResizable(true); HtmlInternalWindow.setMaximizable(true); HtmlInternalWindow.setIconifiable(true); } }); } } |
Categories: Swing
Tags: JDesktopPane, JEditorPane, JInternalFrame, setIconifiable, setMaximizable, setPage, setSizable