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

Component-Specific Conversion Tips

Converting Applets

As mentioned in Step 8: Change calls to the add and setLayout methods, 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.

Don't paint directly in a JApplet (any top-level container really) 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.

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 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 User Interface trail). If your custom component implements a button with an image, you can use a Swing button(in the Creating a User Interface trail) instead. If you've implemented a tree or grid, consider using a Swing tree(in the Creating a User Interface trail) or table(in the Creating a User Interface 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). For more information about implementing painting in custom Swing components, refer to Working with Graphics(in the Creating a User Interface trail). You can remove all code related to double buffering, since Swing provides that automatically.

Other superclasses, such as Component and JComponent(in the Creating a User Interface trail) might also be feasible. Extending JComponent instead of Component/Container gives you automatic double buffering. Extending JPanel instead of JComponent buys you two things: automatic background painting (which you can turn off, if you like, using setOpaque(false) plus the ability to use borders to draw the edges of the component.

Converting Choices

Although both choices and combo boxes fire item events, the conditions under which the events are fired differ. An AWT choice component fires one item event each time the user selects an item from its menu (this behavior is different than that described in the API docs). 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 under these circumstances.

Depending on your needs, you have a few 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

When converting AWT text areas, you normally have to put the Swing text areas 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 instead of getting scrollbars the text area will grow each time a line of text is added to it (depending on the layout manager you use). You can usually remove the row and column arguments for the text area's constructor.

If you put a text area into a scroll pane, then remember to make these other changes to your program:

Swing text components don't have an addTextListener method. Instead register a document listener on the text component's document. For an example of converting an AWT text component to a Swing text component, including how to convert text listeners, refer to Converting TextEventDemo.

If you want to add styled text to your program, convert to a JEditorPane or a JTextPane instead.


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