The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search

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 and How to Use Threads 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` 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. If your custom component implements a button with an image, you can use a Swing button instead. If you've implemented a tree or grid, consider using a Swing tree or table.

> 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.

> `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`. 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:

> - If you want to be notified when the user chooses an already selected item, use an action listener instead of an item listener. Although choices fire only item events, combo boxes fire action events as well.
> - If you're interested in item events, but only care about one per selection, use the event object's `getStateChanged` method to determine whether you're interested in a particular event.

### Converting Lists

> AWT lists and Swing lists differ in many ways:
> - Populating a Swing list is different than populating an AWT list because a standard Swing list is immutable--you cannot add or remove items from it. To populate a Swing list, create a vector, array, or data model with the initial items in it.

>   AWT Code | Swing Code
>   --- | ---
>       List spanish = new List();
>       spanish.addItem("uno");
>       spanish.addItem("dos");
>       spanish.addItem("tres");
>       spanish.addItem("cuatro");
>       String[] listData = { "uno",
>       		      "dos",
>       		      "tres",
>       		      "cuatro" };
>       JList spanish = new JList(listData);

>   If you want a mutable list, you need to provide the list with a custom `ListModel` that is mutable. Refer to Modifying a List for details and an example.

> - AWT lists provide scroll bars automatically. If you want scrolling capability for a Swing list, you need to put the list in a scroll pane. Remember to change the arguments to the `add` method so that it adds the scroll pane, rather than the actual list, to the container. If you are using a `GridBagLayout`, make sure that you then apply the constraints to the scroll pane rather than to the list.

>   AWT Code | Swing Code
>   --- | ---
>   `List spanish = new List(); somecontainer.add(spanish);` | `JList spanish = new JList(listData); JScrollPane scrollPane = new JScrollPane(spanish); somecontainer.add(scrollPane);`

> - Although `List` generates action events, `JList` doesn't. To listen for changes to the selection on a `JList` use a `ListSelectionListener`. To listen for single, double, or triple clicks, use a mouse listener.

> - A Swing list supports three selection modes, not two.
> 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. For information on what Swing text components have to offer, see Using Text Components.

> 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:

> - Change the `add` call to add the scroll pane, instead of the text area, to the container.
> - If you are using a `GridBagLayout` make sure that you then apply the constraints to the scroll pane rather than to the text area.

> 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, and examples of implementing custom documents are in Creating a Validated Text Field.

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

| Start of Tutorial > Start of Trail > Start of Lesson | Search