Converting to Swing |
[PENDING: this section is under construction. Some of the sections below are our notes and not actual content.]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.[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:
- 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.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)
. [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
andJComponent
might also be feasible. ExtendingJComponent
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
AWT programs populate aChoice
object using theaddItem
method. Swing programs populate aJComboBox
by creating aVector
, an array, or a data model with the initial items in it. Here's an example:
AWT Code Swing 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 aJTextPane
instead.
Converting to Swing |