![]() ![]() ![]() ![]() |
Converting to Swing |
Converting Applets
As mentioned in Step 8: Change calls to theadd
andsetLayout
methods, AWT programs add components directly to theApplet
object and directly set the applet's layout manager. Swing applets, on the other hand, add components to and set the layout manager on theJApplet
'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:
- If you want to use a
FlowLayout
, the Swing version of your program must usesetLayout
on the content pane to specify it.- If you specified
BorderLayout
in your AWT applet, you can remove thesetLayout
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 anOBJECT
/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
toJPanel
. Then move the drawing code frompaint
orupdate
to a method namedpaintComponent
. At the top of that method, you should insertsuper.paintComponent(g)
. For more information about implementing painting in custom Swing components, refer to Working with Graphics. You can remove all code related to double buffering, since Swing provides that automatically.
Other superclasses, such as
Component
andJComponent
might also be feasible. Extending
JComponent
instead ofComponent
/Container
gives you automatic double buffering. ExtendingJPanel
instead ofJComponent
buys you two things: automatic background painting (which you can turn off, if you like, usingsetOpaque(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:
- If you're getting two item events and you're only interested in one of them, call the event object's
getStateChanged
method. Ignore those you aren't interested in.- If you aren't getting any item events (because the user chose the already selected item), use an action listener instead. Combo boxes fire action events as well as item events, while choices fire only item events.
- You can also consider handling low-level events such as mouse events. But use caution when doing this:
JComboBox
is a compound component and low-level events are fired by its subcomponents. The low-level events and the subcomponent that fires them are system-dependent.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.
- 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);Or, if you want a mutable list, you provide the list with a custom
ListModel
that is mutable. Refer to Modifying a Listfor 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 add the scroll pane to the top-level container instead of the list itself.
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 aJList
use aListSelectionListener
. To listen for single, double, or triple clicks use a mouse listener.
- A Swing list supports three selection modes, not two.
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
- Change the
add
call to add the scroll pane to the top-level container 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.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 aJTextPane
instead.
![]() ![]() ![]() ![]() |
Converting to Swing |