Swing

Swing is Sun's latest toolkit for building user interfaces. It is intended to replace the AWT, and developers are being encouraged to migrate to Swing. Swing is included as a standard part of the current production version of Java, now referred to as SDK 1.2.2 (versus JDK), along with the older AWT.

Swing differs from AWT in several important respects.

The most important difference is that Swing has a different architecture and implementation. AWT components are heavyweight -- meaning that each Java component has a corresponding so-called peer component implemented with respect to the native architecture and windowing systems. Thus, Java windows and components have the look and feel of the platform where the program is run. As a result, Java windows on a Mac look like Mac windows and Java windows on a PC look like PC windows. Swing components, for the most part, are lightweight -- implemented in Java. With the exception of a handful, they have no corresponding peers. Thus, programmers can specify which particular look and feel they want, and their components will have that appearance same regardless of the platform where the program is run on. One implications of this difference is that one must be very careful, and should avoid where possible, mixing Swing and AWT components.

Other differences between Swing and the AWT are in the details. Swing includes a number of new components and features. Most are provided through the javax.swing package. For a discussion and illustration of many, see Sun's Visual Index to the Swing Components. It also includes convenient new tools for drawing and image handling and support for drag and drop. In the future, it is likely that most new packages -- at least those developed by Sun -- will build on Swing versus the AWT.

So, it's probably not too soon to begin learning Swing. However, be warned that you can expect some frustrations and you make this change. Swing is much larger than AWT, and several aspects of it are not fully mature. For example, documentation for the current production version (SDK 1.2.2) is not up-to-date. Since installation and the location of class archives are different from earlier versions, this can be frustrating. Also, applets that use SDK 1.2.2 cannot be displayed with current Web browsers without an upgraded plugin, different from earlier Swing plugins.

If all this doesn't scare you off (for now), see Sun's various Java Tutorials to get started. In particular, see Creating a GUI with JFC/Swing. Two additional tutorials that may be helpful are Steve Boles' Java Swing - Part I and Skip Walker's Java Swing - Part II, , prepared by them in the Spring, 1999, Advanced WWW Programming course.


Getting Started

To use the SDK 1.2.2 version of Swing, you need to go through a rather elaborate setup process. the major steps in that process are described here.

Basic Window

In an earlier lesson, we built an basic window in AWT for an applet that includes menus, scrollbars, and an area for drawing or other data. Here, that code is revised to do it in Swing.

Sun's SwingSet Demo

Sun has included as part of the SDK1.2.2 distribution a demonstration applet that illustrates many of the components available in this version of Java.

Component Programing Example

This example illustrates some of the visual programming options supported by Swing components.


References

References useful for this discussion include the following: