he previous chapter described several of the core concepts relating to Swing and
showed the general form of both a Swing application and a Swing applet. This
chapter continues the discussion of Swing by presenting an overview of several
Swing components, such as buttons, check boxes, trees, and tables. The Swing components
provide rich functionality and allow a high level of customization. Because of space
limitations, it is not possible to describe all of their features and attributes. Rather, the
purpose of this overview is to give you a feel for the capabilities of the Swing component set.
The Swing component classes described in this chapter are shown here:
These components are all lightweight, which means that they are all derived from
Also discussed is the ButtonGroup class, which encapsulates a mutually exclusive set
of Swing buttons, and ImageIcon, which encapsulates a graphics image. Both are defined
by Swing and packaged in javax.swing.
One other point: The Swing components are demonstrated in applets because the code
for an applet is more compact than it is for a desktop application. However, the same
techniques apply to both applets and applications.
JLabel and ImageIcon
JLabel is Swing's easiest-to-use component. It creates a label and was introduced in the
preceding chapter. Here, we will look at JLabel a bit more closely. JLabel can be used to
display text and/or an icon. It is a passive component in that it does not respond to user
input. JLabel defines several constructors. Here are three of them:
JLabel(String str, Icon icon, int align)
Search WWH :