Java Reference
In-Depth Information
CHAPTER 8
■ ■ ■
Root Pane Containers
I
n Chapter 7, you looked at working with borders around Swing components. In this chapter,
you'll explore the high-level Swing containers and discover how they differ from their AWT
counterparts.
Working with top-level containers in Swing is a bit different from working with top-level
AWT containers. With the AWT containers of
Frame
,
Window
,
Dialog
, and
Applet
, you added
components directly to the container, and there was only one place you could add them. In
the Swing world, the top-level containers of
JFrame
,
JWindow
,
JDialog
, and
JApplet
, plus the
JInternalFrame
container, rely on something called a
JRootPane
. Instead of adding components
directly to the container, you add them to a part of the
root pane
. The root pane then manages
them all internally.
Why was this indirect layer added? Believe it or not, it was done to simplify things. The root
pane manages its components in layers so that elements such as tooltip text will always appear
above
components, and you don't need to worry about dragging some components around
behind others.
The one container without an AWT counterpart,
JInternalFrame
, also provides some addi-
tional capabilities when placed within a desktop (within a
JDesktopPane
to be specific). The
JInternalFrame
class can be used as the basis for creating a Multiple Document Interface (MDI)
application architecture within a Swing program. You can manage a series of internal frames
within your program, and they'll never go beyond the bounds of your main program window.
Let's begin by exploring the new
JRootPane
class, which manages the internals of all the
top-level containers.
JRootPane Class
The
JRootPane
class acts as a container delegate for the top-level Swing containers. Because
the container holds only a
JRootPane
when you add or remove components from a top-level
container, instead of directly altering the components in the container, you indirectly add or
remove components from its
JRootPane
instance. In effect, the top-level containers are acting
as proxies, with the
JRootPane
doing all the work.
The
JRootPane
container relies on its inner class
RootLayout
for layout management and
takes up all the space of the top-level container that holds it. There are only two components
within a
JRootPane
: a
JLayeredPane
and a glass pane (
Component
). The glass pane is in front, can
be any component, and tends to be invisible. The glass pane ensures that elements such as
235