The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Converting to Swing

Component-Specific Conversion Tips

This section has tips for converting AWT applet, canvas, choice, list, and text components to the Swing equivalents.

Converting Applets

As mentioned in Step 7 of the conversion instructions, AWT programs add components directly to the Applet object and directly set the applet's layout manager. Swing applets, on the other hand, add components to and set the layout manager on the JApplet's content pane. So to convert an applet, you must make the source code changes described in that section.

Furthermore, while FlowLayout is the default layout manager for AWT applets, BorderLayout is the default layout manager for Swing applets. This has two repercussions:

  1. If you want to use a FlowLayout, the Swing version of your program must use setLayout on the content pane to specify it.
  2. If you specified BorderLayout in your AWT applet, you can remove the setLayout statement from the Swing version.

As Step 8 says, don't paint directly in a JApplet because it will be covered by the applet's content pane. Instead of seeing your painting, you'll just see a blank area. The solution is to have a custom component do the painting, and add it to the content pane. See the instructions for converting canvases for tips on choosing a class for the custom component, and moving the paint code to the appropriate method.

Be very aware of thread issues when converting your applet. Because the stop and start methods aren't called from the event-dispatching thread, you should use SwingUtilities.invokeLater in those methods whenever you make a change that might result on a call upon a component. See Threads and Swing(in the Creating a GUI with JFC/Swing trail) and How to Use Threads(in the Creating a GUI with JFC/Swing trail) for more information.

Lastly, your applet's users will probably use Java Plug-in to run your applet. So you will need to convert the <APPLET> tag to an OBJECT/EMBED tag. An automatic HTML converter can be found at the Java Plug-in(outside of the tutorial) site.

Converting Canvases (Custom Components)

Before converting a custom component, check whether you can use a standard Swing component. For example, if your custom component simply displays an image, perhaps with some text, then you can use a Swing label(in the Creating a GUI with JFC/Swing trail). If your custom component implements a button with an image, you can use a Swing button(in the Creating a GUI with JFC/Swing trail) instead. If you've implemented a tree or grid, consider using a Swing tree(in the Creating a GUI with JFC/Swing trail) or table(in the Creating a GUI with JFC/Swing trail).

If no Swing component has the functionality you need, then we recommend that you change the superclass of your custom component from Canvas to JPanel. Then move the drawing code from paint or update to a method named paintComponent. At the top of that method, you should insert super.paintComponent(g). Remove all code related to double buffering, since Swing provides that automatically. For more information about implementing painting in custom Swing components, refer to Working with Graphics(in the Creating a GUI with JFC/Swing trail).

JPanel is not the only possible superclass for custom components. You can use a more specialized superclass, such as JLabel or AbstractToggleButton. Or you can use a less specialized superclass, such as Component, Container, or JComponent(in the Creating a GUI with JFC/Swing trail). Extending JComponent instead of Component or Container gives you automatic double buffering and access to Swing features such as borders. Extending JPanel instead of JComponent buys you automatic background painting, which you can turn off if you like with setOpaque(false).

Converting Choices

AWT choices are equivalent to uneditable Swing combo boxes. Although both choices and combo boxes fire item events, the conditions under which the events are fired differ. A choice fires one item event each time the user selects an item from its menu. The Swing combo box component fires one item event each time the state of an item changes. Thus a combo box fires two item events when the user chooses a new item from the combo box: one because the currently selected item's state changed to deselected and one because the chosen item's state changed to selected. Furthermore, although a choice fires an item event when the user chooses the already selected item, a combo box fires no events in this case.

You have a couple of options:

Converting Lists

AWT lists and Swing lists differ in many ways: For an example of converting a program that contains a list, refer to Converting ListDemo.

Converting Text Components

The Swing text components have much richer functionality than the AWT text components. You can easily display styled text, such as HTML text, using a editor pane or text pane(in the Creating a GUI with JFC/Swing trail). For information on what Swing text components have to offer, see Using Text Components(in the Creating a GUI with JFC/Swing trail).

When converting a text area, be aware that Swing text areas aren't automatically placed in scroll panes. You normally should put the Swing text area in a scroll pane and set the scroll pane's preferred size. If you forget to set the preferred size of the scroll pane, then depending on the layout manager you use, the scroll pane might grow each time a line of text is added to the text area -- and the user won't see scroll bars. When you add a text area to a scroll pane, remember to make these other changes to your program:

Swing text components support different listeners than AWT text components. For example, Swing text components don't have an addTextListener method. Instead you should use a custom document or register a document listener. If you have a key event listener registered on an AWT text component, then you should probably replace it with a custom document. Documents and document listeners are discussed in General Rules for Using Text Components(in the Creating a GUI with JFC/Swing trail), and examples of implementing custom documents are in Creating a Validated Text Field(in the Creating a GUI with JFC/Swing trail).

For an example of converting an AWT text component to a Swing text component, including how to convert text listeners, refer to Converting TextEventDemo.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.