As discussed in the previous AWT section,
to provide a more powerful graphical user interface Sun and collaborators
developed a set of lightweight component classes under the name
Swing that initally ran under Java 1.1 as an optional set
of packages and then with Java 1.2 became a part of the standard
Java set of classes.
Note: Swing is considere a standard
extension and not part of the core Java language, i.e.
the package name starts with javax instead of java.
It is included with the J2SE and J2EE kits and their corresponding
packages, such as javax.comm discussed in Chapter
23, pare not included with these SDKs and are called optional
packages. They are not available necessarily available for the
same range of platforms as the core language packages.
Swing brought a huge improvement in the GUI with new capabilities
that ranged from buttons with user selected icons to checkboxes
to tables with multiple columns and rows. The Look & Feel,
that is, the color scheme and design, of the components could also
See Sun's Visual
Index to the Swing Components for a look at the great variety
of components available.
Swing and came as part of the Java Foundation Classes (JFC)
set of packages that also include :
- Pluggable Look & Feel - the style of the components,
such as their color scheme and design, can be customized and can
look the same regardless of the platform.
- Accessibility API - modifies the interface for easier
use by the disabled.
- Drag and Drop - provides for moving data between Java
programs and other programs running on the native host.
- Java2D - an expanded set of drawing tools. These work
within both the Swing and AWT components. We will discuss some
basic Java2D techniques in the Chapter
Swing Class Hierarchy
The Swing classes build upon the lower level classes of the original
graphics packages. As the diagram shows below, the Swing user interface
components, which always begin with the letter "J", extend
from the Container
This diagram shows a subset of the Swing components
(in dark gray)
and how they extend the AWT (in light gray) components.
Note: The diagram omits some Swing
components like JTable.
Also, there are various supporting classes in javax.swing
packages that don't begin with "J".
subclasses are lightweight (see AWT
page), so they essentially run within a single heavyweight high
level component, such as JFrame and JDialog, and draw and re-draw
themselves completely within Java, no native code peer components
Combined with the event handling process described in Chapter
7: Java, Swing components provide very flexible and elaborate
GUI tools. One can also develop custom components in a straight
forward manner by extended either JComponent
or one of its subclass components.
It is now generally recommended that everyone switch their Java
user interface design from AWT to Swing for all serious program
development for PC and equivalent platforms.
Drawbacks to Swing
However, there are some practical drawbacks
- The number of Swing classes and their depth and complexity is
far greater than the AWT.
- Programs can take up large amounts of memory .
- Many browsers in use still do not include Java 1.2 (or later)
- Browsers plug-ins to run applets with classes from 1.2 or later
are available for some platforms but not all. Also the plug-ins
require a large download the first time (then cached for later
use). So this can be a problem for users on slow connections .
Note: In situations where these drawbacks
prevent the implementation of Swing based GUI, such as when developing
programs for small platforms, it can be necessary to remain within
the older AWT framework. See the Chapter
6: Supplements section for a brief overview creating user interfaces
purely with the standard AWT components.
We will cover a number of aspects of Swing in this and later chapters.
However, Swing includes an enormous number of classes and so we
can only touch on a fraction of their capabilities.
One popular reference for Swing prorgamming, Graphic
Java 2: Mastering the JFC, requires more than 1600 pages
to give in depth descriptions of all the Swing classes. To supplement
the materials here, we recommend the on line Swing
Tutorial at java.sun.com.
References and Web Resources
Latest update: Nov. 3, 2006