Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JDestopPane & JInternalFrame in Java Swing

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:

JDesktopPane and JInternalPane Example
JDesktopPane and JInternalPane 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.

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.

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.

JInternalPane Code
JInternalPane Code

Code Explanation

  1. HtmlWindow Derives from the JInternalFrame so that it can be added to the JDesktopPane.
  2. The snippet creates the JEditorPane to show html content in it.
  3. This snippet creates a JScrollPane to manage the content of the JEditorPane. 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.
  4. Here, we get the system path to the html file in URL format.
  5. 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:

Display Java Swing JInternalFrame
Display Java Swing JInternalFrame

Code Explanation

  1. 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.
  2. 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 the add method.
  3. 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 and ly. The pack method will ask the layout manager to pack all the components in the JInternalFrame and, if needed, resize it.
  4. 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

Creating MDI Application in Java Swing

8. Code Reference




Categories: Swing

Tags: , , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.