Contents

Swinging Duke
Feedback Button
Left ArrowRight Arrow

The JLayered Pane and JInternalFrame Classes

 

JLayered Pane and JInternalFrame are a pair of Swing classes that can be used together to create child frame windows inside parent frame windows. With these two classes, you can create layered internal frame windows for what are sometimes referred MDI (multiple-document interface) applications.

JLayeredPane and JInternalFrame create windows that are 100% pure Java implementations of native MDI-style windows. Because the JLayeredPane and JInternalFrame classes have Swing's unique pluggable look-and-feel design, you can use them to create MDI-style windows that have the exact look and feel of any operating system you choose.

There is no requirement that the JLayeredPane and JInternalFrame classes be used together, but they were designed to be used together, and in practice, that is the way they generally are used.

Internal frame windows created using the JInternalFrame class have title bars and can optionally be provided with resizing boxes, close boxes, and maximizing and iconifiying boxes. They can also be equipped with menus. Because of these features, JLayeredPane/JInternalFrame windows have everything they need to be used in MDI applications.

Layer Numbers of Internal-Frame Windows

As the following illustration shows, a JLayeredPane component can contain multiple JInternalFrame components. Each internal-frame window that appears inside a layered-pane object has an arbitrary layer-number property (expressed as an integer) that specifies its place in whatever hierarchy of layered frames appears along with it in its layered-pane window.

If a JLayeredPane component contains multiple internal-frame components, the internal-frame component with the highest layer number is the topmost frame. In turn, the positions of all other frames inside the parent pane depend upon their layer numbers, with layer numbers falling in descending order, and with the lowest layer number belonging to the lowest frame in the hierarchy.

The preceding screen shot illustrates the use of layer numbers in a JInternalFrame/JLayeredPane component hierarchy. Note that the topmost frame has a layer designator of 5, while the middle frame has a layer designator of 3 and the frame at the bottom has a layer designator of 1. That's because layered frames with descending layer numbers appear in descending order inside a layered-pane component, with frames that have higher layer numbers appearing on top, and frames with lower layer numbers appearing lower in the hierarchy.

It is important to remember that it's the order of the layer numbers that matters, not the exact numbers themselves. For example, in the preceding picture, the appearance of the three internal frames would be identical if the top frame had a layer number of 100, the middle frame had a layer number of 64, and the bottom frame had a layer number of 43.


NOTE: When you want to add internal-frame components to a layered-pane component, you generally place them in the layered-pane window's content pane, as shown in the preceding illustration. (The content pane is the rectangular area nested inside the title bar, the scroll bars, and any other other controls and decorations that may lie along the edges of the parent pane. This area is known as the client area in the world of Windows 95/Windows NT programming).


Using JLayered Pane and JInternalFrame

In Swing, JLayeredPane is a subclass of the JComponent class. As you have seen, a JLayeredPane object can contain multiple JInternalFrame components. These components are usually displayed inside the JLayeredPane object's content pane (see preceding note).

Layer Order Numbers and AWT Order Numbers

Because JInternalFrame components are AWT components, they have AWT order numbers as well as a layer order numbers when they appear inside a JLayeredPane components. If multiple internal-frame components are assigned both layer numbers and AWT order numbers, their layer numbers override their AWT order numbers.

The following illustration shows how layer numbers and AWT order numbers might work if several internal-frame components appeared inside a JLayeredPane component and some of the internal frames had duplicate layer numbers. The picture shows what happens if, for example,

As you can see, the two frames with the highest layer number appear together on top, the two frames with the next-lowest layer numbers appear together in the middle, and the frame with the lowest layer number appears on the bottom. If multiple frames appear within any particular layer number, as they do here, they are arranged in their AWT component order within the layer in which they appear.

In the AWT-order hierarchy, components appear in the same order that applies in the JLayeredPane/JInternalFrame hierarchy, with the highest AWT order number on top, and with descending AWT order numbers appearing in descending order.

Using Internal Frames

When you create the first JInternalFrame component that a JLayeredPane will contain, your new JInternalFrame component is assigned a default layer number of 0. Before you add other internal-frame objects to a layered-pane component, you should be sure to check each internal frame's default layer number and manually change its layer number, if necessary, to reflect the position in the frame hierarchy that you want it to have.

You can set the layer number of a JInternalFrame window by calling JLayeredPane.setLayer() method. You can also call setLayer()to move any existing internal-frame window from one layer to another.

You can move a window to the topmost position in a window hierarchy by calling moveToFront(), and you can move it to the lowest position in a window hierarchy by calling moveToBack().

Other Methods

Other methods that can be used with layered-pane components and internal-frame windows are:

To learn about other methods used by the JLayeredPane and JInternalFrame classes, see their respective APIs.


JLayeredPane's Helper Classes

Three helper classes that are used with the JLayeredPane and JInternalFrame classes are the JDesktopPane, DesktopManager, and JDesktopIcon classes.

The DesktopManager is responsible for performing a number of different kinds of operations on JInternalFrame windows, such as resizing, closing, moving, iconifying, and so on. The DesktopManager has no user interface; its job is not to interact with the user, but to manage important JInternalFrame operations.

The JDesktopPane class is a sublcass of JLayeredPane. Along with methods and fields that it inherits from JLayeredPane, JDesktopPane comes equipped with its own DesktopManager. But you can use a different DesktopManager if you like.

A JInternalFrame usually exists inside a JDesktopPane, so when it receives a request to perform some action such as closing, resizing, or the like, it sends the request back up to its DesktopManager, which then handles the requested action. This setup is what allows you to change the look and feel of an internal-frame window. To implement different L&Fs, you use different DesktopManagers. Usually, when you change L&Fs, your JDesktopPane goes and finds a new DesktopManager. But you can also set DesktopManagers manually.

Another class used by JInternalFrame is the JDesktopIcon. When a JInternalFrame component is iconified, the icon that it uses is specified using the JDesktopIcon class. For more information on the JDesktopIcon class, see the JDesktopIcon API.


Version 0.5. Last modified 10/1/97.
Copyright © 1995-97 Sun Microsystems, Inc. All Rights Reserved.