Swing Components and the Containment Hierarchy
Some of the top-level containers offered by swing are -
JApplet
, JDialog
, and JFrame
. Because
of the intricacies involved in making lightweight and heavyweight components
work together in Swing, you can't just add() anything you like to a JFrame,
JApplet, or JDialog object; first, you must get something called a
"content pane," and then you can add Swing components to that.
The different types of panes that
are part of container are as follows -
The Root Pane
In general, you don't directly create a
JRootPane
object.
Instead, you get a JRootPane
(whether you want it or not!) when you instantiate JInternalFrame
or one of the top-level Swing
containers, such as JApplet
, JDialog
, and JFrame
. Its a
lightweight container used behind the scenes by these top-level containers. As
the preceding figure shows, a root pane has four parts:
i) The layered pane
Serves to position its contents, which consist of the content pane
and the optional menu bar. Can also hold other components in a specified Z
order.
JLayeredPane
adds depth
to a JFC/Swing container, allowing components to overlap each other when
needed. An Integer
object specifies each component's depth in the container, where
higher-numbered components sit "on top" of other components. It allows for the definition of a several
layers within itself for the child components.JLayeredPane
manages its list of children like Container
, but allows
for the definition of a several layers within itself. Children in the same
layer are managed exactly like the normal Container
object, with
the added feature that when children components overlap, children in higher
layers display above the children in lower layers.
ii) The content pane
The container of the root pane's visible components, excluding the
menu bar.
iii) The optional menu bar
The home for the root pane's container's menus. If the container has
a menu bar, you generally use the container's
setJMenuBar
method to
put the menu bar in the appropriate place.
iv) The glass pane
Hidden, by default. If you make the glass pane visible, then it's
like a sheet of glass over all the other parts of the root pane. It's
completely transparent unless you implement the glass pane's
paint
method
so that it does something, and it intercepts input events for the root pane.
The glass pane is useful when you want to be able to catch events or paint over
an area that already contains one or more components. For example, you can
deactivate mouse events for a multi-component region by having the glass pane
intercept the events. Or you can display an image over multiple components
using the glass pane.
0 comments: