Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Converting to Swing

Some Conversion Examples

This section provides the AWT and Swing versions of several example programs and talks about the interesting aspects of the conversion. The discussions don't analyze the conversions line-by-line. However, you can see the differences between the AWT and Swing versions of each program, by using a program such as UNIX's diff utility to compare both versions of the source code.

Converting ButtonDemoApplet

The first AWT to Swing conversion example we'll look at is a simple applet that contains three buttons. Here are two snapshots. The first shows the AWT version of the applet running. The second shows the Swing version.

AWT Version

Swing Version

You'll notice that the programs look a little different.

  1. The buttons have a different border.
  2. The applets use different fonts for the text.
  3. The buttons in the Swing applet have icons as well as text.
  4. The buttons in the Swing applet show the button's mnemonic.
  5. The Swing applet is bigger.

The first two differences exist simply because the AWT and the Java Look & Feel used by the Swing program draw buttons differently and have different default fonts. The Java Look & Feel is the look and feel that Swing programs use unless either the user specifies a different one or you explicitly set the look and feel in your program's code.

Differences 3 and 4 are because we chose to take advantage of two Swing button features not supported by AWT buttons: images and mnemonics.

The final difference is a side effect of the previous four differences. If you program is an applet, remember to modify the <APPLET> tag to adjust for any size changes in your program.

Although the programs look somewhat different they behave the same. When you click the left button, it disables the middle button and itself, and enables the right button. When you click the right button, it disables itself, and enables the middle and left buttons.

The following table links to the complete source code and an HTML file containing a sample <APPLET> tag for each version of the program. Compare the code and the <APPLET> tags to see the differences between the two programs.

[PENDING: Acknowledge EMBED and OBJECT tags, which you create for Plug-in using the converter the Plug-in team provides.]

  Source Code <APPLET> Tag *
AWT ButtonDemoApplet.java ButtonDemoApplet.html
Swing ButtonDemoApplet.java
left.gif
middle.gif
right.gif
ButtonDemoApplet.html
* Note that if your browser isn't configured to run 1.1 or Swing programs, visiting the .html files listed in the table will produce errors. We provide the files so that you can look at the applet tags. Use shift-click to download the files.

Converting AnimatorApplication

AnimatorApplication is a template for animation programs. This particular implementation "animates" a string by changing the string periodically. The program can be easily modified to animate images in the same manner.

This section discusses two different solutions to converting the animator program. The first solution takes a minimalistic approach--the code is changed only as necessary to get the program to run with Swing. The second solution is more complete--it changes the code to adjust for differences in the way the AWT and Swing programs paint and to take advantage of a new class in Swing.

The AWT AnimatorApplication class extends Frame. The frame's paint method uses drawString to paint a string in the frame. A thread periodically wakes up, changes the string to the next value, and repaints the frame.

Both Swing versions of the AnimatorApplication class extend JFrame.

The minimalist version of the program paints the same way the AWT version paints--by calling the drawString method. However in Swing the painting code belongs in a method named paintComponent. Furthermore, because a JFrame has a content pane, the painting done in its paintComponent method has no effect (the content pane just paints right over it). So the painting code had to be moved out of the JFrame. Instead, the program defines a JPanel subclass, AnimappPanel, to do the painting, and uses an instance of AnimappPanel as the JFrame's content pane.

The second solution is a more complete Swing solution. Instead of creating a JPanel subclass to draw a string, this solution uses a JLabel, which is specially tuned [PENDING: is "tuned" the right word to use?] for painting strings. Additionally, the second solution uses Swing's Timer class instead of using a Thread that sleeps periodically.

Here are the three different versions to compare:

  Source Code
AWT AnimatorApplication.java
Minimalist Swing Solution AnimatorApplication.java
Complete Swing Solution AnimatorApplicationTimer.java

Converting TextEventDemo

Here's a snapshot of the Swing version of the TextEventDemo applet. The AWT version looks similar.
This program has a text field and a text area on the left, both of which are editable. The program listens for changes to the text on the text field and the text area and logs changes in the uneditable text area on the right.

AWT text areas supported scrolling directly. Swing text areas don't. So, the Swing version of this example creates a JScrollPane for each text area. The program uses a GridBagLayout to position the components. During the first pass of the conversion, we forgot to change the setConstraints call to set the constraints on the scroll pane instead of the text area and ended up with a tiny little scroll pane.

The AWT version of this program registers a text listener with addTextListener to listen for changes on the text components. The text listener implements a single method called textValueChanged.

Swing text components do not have an addTextListener method. Instead, the Swing version of this program has to register a document listener on each text component's document. A document listener implements three methods: insertUpdate, removeUpdate, and changedUpdate. Allowing a program to listen for specific types of changes.

Finally, the AWT version of the program had to explicitly force the change log (the text area on the right) to scroll down to display the text that was added to the bottom of the text area. This code could be removed in the Swing version because Swing text areas in scroll panes handle this automatically.

Here's the code and <APPLET> tags to compare:

  Source Code <APPLET> Tag *
AWT TextEventDemo.java TextEventDemo.html
Swing TextEventDemo.java TextEventDemo.html
* Note that if your browser isn't configured to run 1.1 or Swing programs, visiting the .html files listed in the table will produce errors. We provide the files so that you can look at the applet tags. Use shift-click to download the files.

Converting Converter

The Converter is an application that converts distance measurements between metric and U.S. units. Here are snapshots of the AWT and Swing versions of the program:

AWT Converter Swing Converter
AWT Converter Swing Converter

Here's the source code for both versions:

AWT Converter Source Swing Converter Source
Converter.java
ConversionPanel.java
Unit.java
Converter.java
ConversionPanel.java
Unit.java
ConverterRangeModel.java
FollowerRangeModel.java
DecimalField.java
FormattedDocument.java

The main method for both versions is in the Converter class. Both versions use two instances of ConversionPanel--one for the metric system controls and one for the U.S. system controls. The Converter program keeps several instances of Unit, each of which contains a multiplier for a particular conversion.

The Swing version of the program has four additional classes. These provide custom data models for the text fields and the sliders.

The Anatomy of a Swing-Based program(in the Creating a User Interface trail) dissects the Swing version of the Converter program. Read that section to familiarize yourself with the program before proceding.

The Swing version of this program takes advantage of the Swing API and improves upon the AWT version in these ways:


Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Converting to Swing