The
Swing Tool Set
By Mark Andrews
The article titled "Introducing
Swing" explained what Swing is (an extension of AWT)
and what it isn't (a replacement for the AWT tool set).
This
article takes a broader, more topographical overview of Swing. It
lists and describes all the UI component
classes used in Swing (and some of the most important non-UI
classes, too), and it introduces some of the interfaces that Swing
components implement.
It also describes and illustrates the inheritance
hierarchy into which Swing's classes are organized.
To see what all of Swing's UI components look like,
and to find out where to look for more information about each one,
see the article titled "Component
Gallery."
The Swing component hierarchy is similar in some ways
to the AWT hierarchy. But on close examination, you'll see quite
a few differences.
The Swing tool set is a comprehensive library of more
than 250 classes and more than 75 interfaces for creating lightweight,
100% Pure Java GUI components.
Swing has more than twice as many components as AWT
-- it is a comprehensive library of more than 250 classes and more
than 75 interfaces for creating lightweight, 100% pure Java GUI
components.
But in some ways, the hierarchy of the Swing component
set has actually been simplified.
The following diagram shows
just a small part of the Swing component hierarchy. But it also
shows some important differences between the way class inheritance
works in Swing and the way it works in the AWT component set.
UI classes and non-UI classes
In this diagram, two features of Swing are immediately
apparent. All Swing component classes have names that start with
"J," and all the component classes shown in the diagram
descend from a class named JComponent.
Don't conclude from this diagram, however, that all
Swing classes are "J" classes, or that all of them descend
from JComponent. Swing has two kinds of classes -- UI classes
and non-UI classes -- and only Swing's UI classes are "J"
classes that descend from JComponent. Swing also has lots of non-UI
classes that don't descend from JComponent and don't have names
that begin with the letter J -- but we won't discuss any non-UI
classes in this article. This article is about Swing components,
so it focuses exclusively on Swing's UI classes.
And what's difference between UI classes and non-UI
classes? The answer is that UI classes create visible components,
such as buttons and menus, which applications can display on a monitor
screen. Non-UI classes provide services for UI classes, and perform
other kinds of vital operations, but don't have user interfaces
and don't display anything on the screen.
Swing's event classes and model classes are examples
of non-UI classes. You'll use many different kinds of non-UI classes
when you start working with Swing.
Inheritance differences between
AWT and Swing
The diagram presented above
shows how a number of Swing's UI classes descend from the JComponent
class. If you're familiar with AWT, it might appear at first glance
that Swing components inherit from the JComponent class in just
about the same way that AWT components used to inherit from the
Component class in pre-Swing versions of AWT.
But if you refer back to the diagram again, and look
at it very closely, you may notice some interesting differences
between the way Swing components inherit from the JComponent class
and the way AWT components used to inherit from the Component class.
Changes in button-component
hierarchy
You can observe several major differences between
Swing and AWT by examining the way in which button inheritance works
in the two tool sets. In AWT, there are only two button classes
-- the Button and CheckBox classes -- and both Button and CheckBox
inherit directly from the Component class, like this:
In Swing, two new kinds of buttons have been added,
and the button-component hierarchy tree has been rearranged. The
Swing button hierarchy looks like this:
Notice that Swing has both a radio-button class and
a check-box class. In pre-Swing versions of AWT, there was no radio-button
class: the CheckBox class pulled double duty, serving as both a
check-box class and a radio-button class. But Swing now has a real
radio-button class (JRadioButton) as well as a check-box class (JCheckBox),
making both kinds of buttons considerably easier to create and implement.
In Swing, both JRadioButton and JCheckBox descend
from another new button class named JToggleButtonapi,
which has three states: pressed,
enabled, and selected. (Actually, all buttons
have these three states; however, JButton ignores the selected
state.)
All of Swing's button classes -- JButton, JToggleButton,
JRadioButton, and JCheckBox -- descend from an abstract class named
AbstractButtonapi,
which neatly encapsulates their common features and capabilities.
The AbstractButton class descends directly from the JComponentapi
class.
Changes in menu-component hierarchy
There are many other differences between the Swing
class hierarchy and the AWT class hierarchy, but we'll take a quick
look at just one more class tree -- the menu tree -- before we move
on to another topic.
You may know that in pre-Swing AWT, menu classes did
not descend from the Component class, but from the AWT's Object
class, in an arrangement that looked something like this:
In Swing, all menu classes do descend from JComponent,
in a more complex tree that also encompasses the button tree that
you saw in the preceding section:
Because the button and menu trees are linked together
in this way in Swing, it's easier to coordinate button events and
menu events in Swing than it was in pre-Swing versions of AWT.
In fact, Swing has a new interface -- called the Action
interface -- that can listen for menu and button events at the same
time. This means that in Swing, you can synchronize and coordinate
menu events and button events in a way that was never possible in
pre-Swing versions of AWT.
In a Swing program, for example, a single mouse click
can activate or deactivate both a menu item and a corresponding
toolbar button. You'll see exactly how that works in an article
on Action events soon to be published in The Swing Connection.
Swing, like any well-behaved collection of Java software,
groups its classes and interfaces into packages. Swing's
packages, and their contents, are:
- The Accessibility package: This package defines a contract
between Java UI objects (such as screen readers, Braille terminals,
and so on) and screen access products used by people with disabilities.
Swing components fully support the accessibility interfaces
defined in the accessibility package, making it easy to write
programs with Swing that people with disabilities can use. The
design of the accessibility package also makes it easy to add
accessibility support to custom components. These custom components
can extend existing Swing components, can extend existing AWT
components, or can be lightweight components developed from
scratch.
- The Swing component package
(javax.swingapi):
The Swing component package is the largest of Swing's five packages.
As Swing's initial beta release drew near, the Swing component
package contained 99 classes and 23 interfaces. With a couple
of exceptions, the Swing component package is also the package
that implements all the component classes used in Swing. (The
exceptions are JTableHeaderapi,
implemented in the javax.swing.tableapi
package, and JTextComponentapi,
implemented in javax.swing.textapi.
) Swing's UI classes (those that have names beginning
with "J") are classes that are actually used to implement
components; all other Swing classes are non-UI classes that
provide services and functionalities to applications and components.
For more details, see the section headed "Varieties
of Swing classes."
- The basic package (javax.swing.plaf.basic): The basic
package is the second largest package in the Swing set. At press
time, it contained 57 classes (but no interfaces). This package
defines the default look-and-feel characteristics of Swing components.
By subclassing the classes in the basic package, you can create
components with your own customized pluggable L&F designs.)
- The beaninfo package (javax.swing.beaninfo):
Defines the SwingBeanInfo class, the superclass for all Swing
BeanInfo classes. This package provides default implementations
of the getIcon()
and getDefaultPropertyIndex()
methods, as well as utility methods like createPropertyDescriptor(),
which makes it possible to write BeanInfo implementations. This
class is meant to be used along with GenSwingBeanInfo(),
a BeanInfo-class code generator.
- The border package (javax.swing.borderapi):
This package contains one interface and nine classes that you
can subclass when you want to draw a specialized border around
a component. (You don't have to touch this package when you
create components with the default borders prescribed by whatever
look and feel you are using. Swing draws default borders automatically
around standard components.)
- The event package (javax.swing.eventapi):
The event package defines Swing-specific event classes. Its
role is similar to that of the java.awt.event package.
Under most ordinary conditions, you'll probably never have
to subclass the components provided in the event package. But
if you ever need to create a component that needs to monitor
some other particular component -- perhaps in a special way
-- you can tap one of the classes provided in this package.
Swing's JListapi
class, for example, uses this package to determine when the
user selects an item in a list.
- The multi package (javax.swing.plaf.multi): The multi
package contains Swing's Multiplexing UI classes (delegates),
which permit components to have UIs provided by multiple user-interface
(UI) factories.
- The pluggable L&F package (javax.swing.plaf): The
pluggable L&F package contains the classes that Swing uses
to provide its components with the pluggable look-and-feel capabilities.
Most developers will never need this package. However, if you're
building a new look and feel and you can't take the conventional
approach of just subclassing the classes in the swing.plaf.basic
package, you can start with the abstract classes in swing.plaf.
- L&F-specific packages that became available with the release
of JFC 1.1 (Swing 1.0) include:
- javax.swing.plaf.metal.
- javax.swing.plaf.motif.
- javax.swing.plaf.windows.
In addition, a MacOS for Macintosh computers is available
separately. For details, see the Java
Software Web site.
- The table package (javax.swing.tableapi):
The table package contains several low-level classes that Swing
uses to help you build, view, and manipulate tables. More information
about the classes in this package will be provided in a special
article on tables in an upcoming issue of The Swing Connection.
- The text package (javax.swing.textapi):
This package contains classes and interfaces that deal with
components which contain text.
- The HTML package (javax.swing.text.htmlapi):
This package contains an HTMLEditorKitapi
class that implements a simple text editor for HTML text files.
- The RTF package (javax.swing.text.rtfapi):
This package implements a simple text editor for RTF (rich text
format) files.
- The tree package (javax.swing.treeapi):
The classes in this package are used to create, view, and manage
tree components.
- The undo package (javax.swing.undoapi):
Developers of packages with undo capabilities -- for example,
text editors -- may need to know about the classes and methods
defined in this package.
As noted previously, Swing's UI classes create components
and controls that you can see on the screen, while Swing's non-UI
classes provide vital services and functionalities to Swing's control
classes and the applications that use them.
Some non-UI classes -- such as ButtonGroupapi
and ImageIconapi
-- are defined in the Swing component package, along with Swing's
component classes. But most of Swing's non-component classes are
implemented in the other packages listed in the section titled "What's
in the Swing packages "
presented earlier this article.
JComponentapi,
an abstract class, is the root class of almost of of Swing's user-interface
("J") components. JComponent's root class is the Object
class, which is defined in the java.lang.Object file. JComponent
also inherits from the AWT Container and Component classes, as shown
in the following diagram. JComponent implements the Accessibilityapi and Serializable
interfaces.
What the JComponent
class provides
Swing's UI components inherit the following features
and capabilities from the JComponent class:
- Pluggable L&F : The JComponent class provides all
JComponent-derived component classes with their "pluggable
look-and-feel" technology, which allows the developer (or,
optionally, the user) to specify or change the appearance and
behavior of the Swing components used in an application, even
while the program is being executed.
- Extensibility : JComponent and its descendants have
built-in technology that lets the developer combine and extend
existing components to create new, customized components.
- Smart trapping of keyboard events : JComponent provides
its descendants with comprehensive keystroke-handling capabilities.
Instead of trapping every keystroke and expecting you to discard
the ones you aren't interested in, JComponent-derived classes
can use methods implemented by the KeyStrokeapi
class to trap just the keystrokes that you do care about, and
can automatically initiate predetermined actions on specified
components.
- Customization of component borders : Any descendant
of the JComponent class can display a customized border by calling
the setBorder() method. JComponent-derived classes
can call setBorder() to create both decorative borders
and non-decorative borders (that is, borders that provide just
margins and padding).
Look-and-feel implementations generally initialize
a component's border property when the component is constructed.
However, you can always override the initial value for the border
property with your own custom border. The BorderFactory class
provides a set of predefined borders that you can use.
- Easy resizing of components : JComponent provides methods
that make it easy for descendants to set the preferred, minimum,
and maximum size for a component.
- Tool Tips -- JComponent supports the use of tool
tips -- short descriptions of components, or tips about
how to use components, that pop up when the cursor lingers over
a component.
- Autoscrolling -- JComponent provides automatic scrolling
in a list, table, or tree when the user is dragging the mouse.
- Support for debugging : JComponent also provides its
descendants with access to Swing's debugGraphicsapi
mechanism, which displays the painting of components in slow
motion so you can check to see whether components are being
drawn properly.
- Support for accessibility technology: This capability
is provided by the Accessibilityapi interface.
- Support for localization. JComponent provides technology
for performing text I/O in languages other than English.
JApplet is a new class used to create applets in Swing
programs. Although JAppletapi
is based on the old AWT Applet class, it is used a little differently.
It provides input and painting mechanisms for its children -- something
that the AWT Applet class did not do. It also provides other kinds
of special support for applets with special requirements, such as
applets that use the JLayeredPane class and the JMenuBar class.
One important characteristic of the JApplet class
is that a JApplet object can have just one child: an object of a
class named JRootPane.
api.
When you want to display a component inside an applet, you can't
simply call the add()
method to add the component to your applet; instead, must call a
JRootPane method called getContentPane().
For
now, you must use the swing.JApplet class instead of the
applet.Applet when you want to create a Swing applet. If
you don't, your applet will not repaint properly. For more details,
see the JApplet
entry in the "Component
Gallery" article.
This section presents a table that lists and summarizes
all visible UI ("J") classes that existed in Swing at
press time. The table contains links that can take you to sources
for more information about each component. Some brief comments about
each class are also provided.
Summary
of Swing's component classes
Component |
Description |
Comments and
information sources |
JAppletapi |
Implements a Java applet. |
The root class for applets. |
JButtonapi |
Implements a button component. |
Swing's basic button class. |
JCheckBoxapi |
Implements a check-box component. |
A class that creates and implements
check boxes. |
JColor-
Chooser |
Displays and manages a color-chooser
dialog. |
In the swing.preview package. |
JComboBoxapi |
Implements a combo-box component. |
Swing's version of the AWT
Choice class. See JComboBoxapi. |
JComponentapi |
The mother of all Swing components. |
Root class for most of Swing's
UI classes. See JComponentapi. |
JDesktopIconapi |
Displays an iconified version
of a JInternalFrameapi. |
See JDesktopIconapi. |
JDesktopPaneapi |
Provides a pluggable DesktopManagerapiobject
for
JInternalFrameapi
objects. |
See JDesktopPaneapi. |
JDialogapi |
Adds enhancements to java.awt.Dialog. |
Contains a root pane as its only
child; must be managed using content panes. See JDialogapi. |
JFileChooser |
Implements a file-chooser
dialog box. |
In the swing.preview package. |
JFrameapi |
Adds enhancements to java.awt.Frame. |
To be covered in an upcoming issue;
meanwhile,
see JFrameapi. |
JInternal-
Frameapi |
Implements a frame object
that can be placed inside a JDesktopPaneapi
object to emulate a native frame window. |
To be covered in an upcoming issue;
meanwhile, see JInternalFrameapi
and JLayeredPaneapi.
|
JLabelapi |
Creates a display area for
displaying read-only text, an image, or both. |
See JLabelapi. |
JLayeredPaneapi |
Can display multiple layered panes
(JInternalFrameapi
objects) inside a frame. |
To be covered in an upcoming issue;
meanwhile, see JInternalFrameapi
and JLayeredPaneapi. |
JListapi |
Allows the user to select one
or more objects from a list. A separate model, ListModelapi,
represents the contents of the list. |
See JListapi. |
JMenuapi |
Implements a menu component. |
Descends from JComponentapi
via JMenuItemapi. |
JMenuBarapi |
Implements a menu bar component. |
Subclasses
JComponentapi. |
JMenuItemapi |
Implements a menu item component. |
See JMenuItemapi. |
JOptionPaneapi |
Displays a dialog box that prompts
the user for a choice and then passes that choice on to the
executing program. |
See JOptionPaneapi. |
JPanelapi |
Provides a generic container for
organizing other components. |
See JPanel
api. |
JPassword-
Fieldapi |
Displays a field in which
the user can type a password. The text of the password does
not appear in the field as it is being typed. |
See the JPassword-
Fieldapi API. |
JPopupMenuapi |
Implements a popup menu. |
Subclasses
JComponentapi. |
JProgressBarapi |
Implements a progress-bar
component. |
See JProgressBarapi. |
JRadioButtonapi |
Implements a radio-button
control. |
A new class in Swing; subclasses
JToggleButtonapi. |
JRadioButton-
MenuItemapi |
Implements a radio-button
menu item. |
Subclasses
JMenuItemapi. |
JRootPaneapi |
Instantiates in a single
step an object made up of a glass pane, a layered pane, an
optional menu bar, and a content pane. |
See JRootPaneapi. |
JScrollBarapi |
Implements a scroll-bar
object. |
See JScrollBarapi. |
JScrollPaneapi |
Implements a scroll-pane
object. |
See JScrollPaneapi. |
JSeparatorapi |
Implements a menu separator
object. |
See JSeparatorapi. |
JSliderapi |
Implements a slider-bar
object. |
See JSliderapi. |
JSplitPaneapi |
Implements a split-pane component. |
See JSplitPaneapi. |
JTabbedPaneapi |
Implements a tabbed-pane
("property-page") component. |
See JTabbedPaneapi. |
JTableapi |
Implements a
table component. |
See the "JTable
Grows Up" article
and JTableapi. |
JTextAreaapi |
Implements a multiline area that
can display editable or read-only text. |
A subclass of JTextComponentapi,
which subclasses JComponentapi. |
JTextPaneapi |
Implements a text component that
can be marked up with attributes to be represented graphically. |
See JTextPaneapi. |
JToggle-
Buttonapi |
Implements a two-stage button
component. |
Subclasses AbstractButtonapi. |
JToolBarapi |
Implements a dockable, floatable
tool bar. |
See JToolBarapi. |
JToolTipapi |
Implements a tool-tip component
(a component that can display a short string, such as the
name of components or a user tip) |
See JToolTipapi. |
JTreeapi |
Displays a set of hierarchical
data in a graphical outline format. |
To be covered in an upcoming issue;
meanwhile, see JTree. |
JViewportapi |
Provides a clipped view of an
arbitrarily large component. Used by JScrollPaneapi. |
See the JViewportapi. |
JWindowapi |
Adds enhancements
to java.awt.Window. |
Will be the topic
of a major article to be published soon; meanwhile, see JWindowapi
and JRootPaneapi. |
|