Java Reference
In-Depth Information
CHAPTER 4
■ ■ ■
Core Swing Components
I
n Chapter 3, you received a quick introduction to the Model-View-Controller (MVC) pattern
used by the components of the JFC/Swing project. In this chapter, you'll begin to explore how
to use the key parts of the many available components.
All Swing components start with the
JComponent
class. Although some parts of the Swing
libraries aren't rooted with the
JComponent
class, all the components share
JComponent
as the
common parent class at some level of their ancestry. It's with this
JComponent
class that
common behavior and properties are defined. In this chapter, you'll look at common function-
ality such as component painting, customization, tooltips, and sizing.
As far as specific
JComponent
descendent classes are concerned, you'll specifically look at
the
JLabel
,
JButton
, and
JPanel
, three of the more commonly used Swing component classes.
They require an understanding of the
Icon
interface for displaying images within components,
as well as of the
ImageIcon
class for when using predefined images and the
GrayFilter
class for
support. In addition, you'll look at the
AbstractButton
class, which serves as the parent class to
the
JButton
. The data model shared by all
AbstractButton
subclasses is the
ButtonModel
inter-
face; you'll explore that and the specific implementation class, the
DefaultButtonModel
.
JComponent Class
The
JComponent
class serves as the abstract root class from which all Swing components
descend. The
JComponent
class has 42 descendent subclasses, each of which inherits much of
the
JComponent
functionality. Figure 4-1 shows this hierarchy.
Although the
JComponent
class serves as the common root class for all Swing components,
many classes in the libraries for the Swing project descend from classes other than
JComponent
.
Those include all the high-level container objects such as
JFrame
,
JApplet
, and
JInternalFrame
;
all the MVC-related classes; event-handling-related interfaces and classes; and much more.
All of these will be discussed in later chapters.
Although all Swing components extend
JComponent
, the
JComponent
class extends the AWT
Container
class, which, in turn, extends from the AWT
Component
class. This means that many
aspects of the
JComponent
are shared with both the AWT
Component
and
Container
classes.
67