Java Reference
In-Depth Information
ability to put a border on any Swing component (as we saw in Chapter 6). This
ability is defined once in the
JComponent
class and is inherited by any class that is
derived, directly or indirectly, from it.
Some component classes, such as
JPanel
and
JLabel
, are derived directly from
JComponent
. Other component classes are nested further down in the inheritance
hierarchy structure. For example, the
AbstractButton
class is an abstract class
that defines the functionality that applies to all types of GUI buttons.
JButton
is
derived directly from it. However, note that
JCheckBox
and
JRadioButton
are
both derived from a class called
JToggleButton
, which embodies the common
characteristics for buttons that can be in one of two states. The set of classes that
define GUI buttons shows once again how common characteristics are put at
appropriately high levels of the class hierarchy rather than duplicated in multiple
classes.
The world of text components demonstrates this as well. The
JTextField
class
that we've used in previous examples is one of many Java GUI components that
support the management of text data. They are organized under a class called
JTextComponent
. Keep in mind that there are many GUI component classes that
are not shown in the diagram in Figure 9.7.
In previous chapters we've extended the component class hierarchy further by
defining panel and applet classes of our own using inheritance. By extending the
JPanel
or
JApplet
class, we create our own classes that automatically have all the
characteristics of those components. Sometimes we then overrode the definition
of a method, such as the
paintComponent
method, to behave in a particular way.
Creating our own panel and applet classes is a classic use of inheritance,
allowing the parent class to shoulder the responsibilities that apply to all of its
descendants. For example, the
JApplet
class is already designed to handle all of
the details concerning applet creation and execution. An applet interacts with a
browser, can accept parameters through HTML code, and is constrained by cer-
tain security limitations. The
JApplet
class already takes care of these details in
a generic way that applies to all applets. The applet class that we write (the one
derived from
JApplet
) is ready to focus on the purpose of that particular pro-
gram. In other words, the only issues that we address in our applet code are those
that make it different from other applets.
SELF-REVIEW QUESTIONS
(see answers in Appendix N)
SR 9.23
True or False, based on the GUI component class hierarchy diagram
displayed in this section.
a. A
JCheckBox
object is a
JToggleButton
.
b. A
JCheckBox
object is an
AbstractButton
.
c. A
JTextField
object is a
JLabel
.
Search WWH ::
Custom Search