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

Component-Specific Conversion Tips

[PENDING: this section is under construction. Some of the sections below are our notes and not actual content.]

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.

[PENDING: Mention the need to convert the <APPLET> tag for Java Plug-in.]

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.

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). [PENDING: see the drawing lesson, when it's converted, for examples.] 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

AWT programs populate a Choice object using the addItem method. Swing programs populate a JComboBox by creating a Vector, an array, or a data model with the initial items in it. Here's an example:

AWT CodeSwing Code
String choiceprefix = "Choice item #";

Choice choice = new Choice();
for (int i = 0; i < 10; i++) {
    choice.addItem(choiceprefix + i);
}

String choiceprefix = "Choice item #";
final int numItems = 10;
Vector vector = new Vector(numItems);
for (int i = 0; i < numItems; i++) {
    vector.addElement(choiceprefix + i);
}
JComboBox comboBox = new JComboBox(vector);

Converting Lists

You need to put lists in a scroll pane, whereas in AWT-land you got them automatically. When populating a list, you cannot do it the same way you populated an AWT list. Instead, you need to create a vector, array, or data model with the initial items in it. [PENDING: note that JList doesn't automatically have scroll bars, unlike List. Also note that JList doesn't generate action events. Instead, you use a mouse listener and check for double clicks.]

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. It's a little bit difficult to figure out the relationship between the old row and column arguments and the preferred size in pixels. This means you can probably leave out the row and column arguments for the text area's constructor. Don't forget to change the add call to add the scroll pane instead of the text area.

If you are using a GridBagLayout make sure that you then apply the constraints to the scroll pane rather than to the text area (otherwise you get a little itty bitty tiny scroll pane, that actually seems to work).

In ContainerEventDemo, I forgot to set the preferred size of the scroll pane, and it grew each time a line of text was added to it rather than getting scrollbars.

Swing text components don't have an addTextListener method. I converted the TextEventDemo to use document listener instead. This might be a useful example on a conversion that isn't 1:1.

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