Java Reference
In-Depth Information
Q :
You say that Swing defines a GUI that is superior to the AWT. Does this mean
that Swing replaces the AWT?
A : No, Swing does not replace the AWT. Rather, Swing builds upon the foundation
provided by the AWT. Thus, the AWT is still a crucial part of Java. Swing also uses
the same event handling mechanism as the AWT (which was described in Chapter
15 ) . Although knowledge of the AWT is not required by this chapter, you need a sol-
id understanding of its structure and features if you seek full Swing mastery.
Components and Containers
A Swing GUI consists of two key items: components and containers . However, this dis-
tinction is mostly conceptual because all containers are also components. The difference
between the two is found in their intended purpose: As the term is commonly used, a com-
ponent is an independent visual control, such as a push button or text field. A container
holds a group of components. Thus, a container is a special type of component that is de-
signed to hold other components. Furthermore, in order for a component to be displayed, it
must be held within a container. Thus, all Swing GUIs will have at least one container. Be-
cause containers are components, a container can also hold other containers. This enables
Swing to define what is called a containment hierarchy , at the top of which must be a top-
level container .
Components
In general, Swing components are derived from the JComponent class. (The only excep-
tions to this are the four top-level containers, described in the next section.) JComponent
provides the functionality that is common to all components. For example, JComponent
supports the pluggable look and feel. JComponent inherits the AWT classes Container
and Component . Thus, a Swing component is built on and compatible with an AWT com-
ponent.
All of Swing's components are represented by classes defined within the package
javax.swing . The following table shows the class names for Swing components (including
those used as containers):
Search WWH ::




Custom Search