Chapter 5. Buttons
Buttons
Buttons
are simple UI components used to generate events when the user
presses them. In AWT, buttons were very basic,
able to display only simple text strings. In much the same way as
JLabel
provided improvements over
java.awt.Label
, the Swing button classes improve
on java.awt.Button
and
java.awt.Checkbox
by introducing the ability to
display icons, text, or both. In this section, we’ll introduce
both the ButtonModel
interface and
DefaultButtonModel
class (which define the state
of the button). Next, we’ll look at the
AbstractButton
class (which defines much of the
functionality for all button types). Finally, we’ll look at
four concrete subclasses of AbstractButton
and see
how they can be grouped together using a
ButtonGroup
.
Figure 5.1 shows the class
hierarchy, with significant relationships
between the button-related Swing classes. Notice that, as we
discussed in the introductory chapters, each button
(AbstractButton
) keeps a reference to a
ButtonModel
, which represents its state.
Figure 5-1. Swing Button class diagram
The JMenuItem
class shown here (and its
subclasses, not shown) is not covered in this chapter. Instead, they
are covered in Chapter 14.
The ButtonModel Interface
The state of any Swing button is
maintained by a ButtonModel
object. This interface defines methods for reading and writing the model’s properties and for adding and removing ...
Get Java Swing now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.