|
|
|
|||||||||||||||||||
|
How to ConvertThis section presents steps you can follow to convert a program to use the Swing components. Each step applies to all programs -- applications and applets, alike -- unless noted otherwise.Step 1: Save a copy of the original program.Copy all of the program's files, including the Step 2: Remove all references to the
|
| AWT Code: |
Button button = new Button("A Button");
|
|---|---|
| Swing Code: |
JButton button = new JButton("A Button");
|
Some Swing components are not source-compatible with the AWT component they replace. So, for some AWT components, you have to rewrite some code when replacing them with Swing components. Conversion Resources has information to help you with conversions that require more than a simple name change.
While looking at the Conversion Resources section, take notes on changes that you might want to make later to take advantage of Swing features. For example, you might want to put images in your buttons, or to share data models between two components. Before making optional changes, though, we recommend that you make the minimal changes necessary to convert your program to Swing. Once you've successfully run the Swing version of your program, you'll be better able to judge how optional changes might improve your program.
add
and setLayout methods.In programs that use AWT components, you add components directly to frames, dialogs, and applets. Similarly, you set the layout manager directly on those containers. In contrast, when you use the Swing versions of those containers, you add components to (and set the layout manager on) something called a content pane. Here's an example of converting some simple, typical code:
AWT Code: frame.add(panel, BorderLayout.CENTER);Swing Code: frame.getContentPane().add(panel, BorderLayout.CENTER);Here is a slightly more complex example:
AWT Code: frame.setLayout(new FlowLayout());
frame.add(button);
frame.add(label);
frame.add(textField);Swing Code: Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
contentPane.add(label);
contentPane.add(textField);Here is an example of converting an applet. Note that the default layout manager for a content pane is
BorderLayout-- not theFlowLayoutused byApplet.
AWT Code: setLayout(new BorderLayout());
add(button, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);Swing Code: Container contentPane = getContentPane();
contentPane.add(button, BorderLayout.NORTH);
contentPane.add(panel, BorderLayout.CENTER);For more information about content panes, see Swing Components and the Containment Hierarchy
. For more information about converting applets, refer to Converting Applets.
paint and
update methods.For Swing components, custom painting code goes in thepaintComponentmethod. Other ways to paint are using standard or custom icons (which generally are used to display pictures) and borders. If your component has painting code that draws the component's edge, you might well be able to replace that code with a border.If your program has a
Frame,Dialog, orAppletsubclass that implementsupdateorpaint, then you need to move the painting code into another component, entirely. The reason is that each of these containers is covered by a content pane, which hides any painting the container might do. Exactly which component the painting code should move to depends on the type of painting. If the container's painting can be performed by an icon, than the component can be a standard label with an icon. Otherwise, the component should generally be aJPanelsubclass that either performs the painting in itspaintComponentmethod or uses a border to do the painting. You then add the component to the frame, dialog, or applet's content pane, as described in Step 7.See Working with Graphics
for details about painting.
After you've modified the source code as indicated in the prevous steps, use the compiler to try to compile your program. You can find instructions in Compiling and Running Swing Programs. During this step, you are basically getting the compiler to help you identify any conversions you might have overlooked. Don't expect your program to compile the first time!
The compiler can help you do the following:
Fix all problems reported by the compiler until the program compiles.
- Find each AWT component that you forgot to convert to its Swing equivalent. If you removed all of the
java.awtimport statements from your program as suggested in step 2, the compiler displays an error message like this for each AWT component remaining in your program:TextEventDemo.java:23: Class Button not found in type declaration. Button button = new Button("Clear"); ^- Identify which AWT classes your program still needs. If you removed all of the
java.awtimport statements from your program, the compiler displays an error message like this for AWT classes you still need:The AWT classes you might still need in a Swing program are layout managers, graphics-related objects such asTextEventDemo.java:17: Class BorderLayout not found in type declaration. BorderLayout layout = new BorderLayout(); ^ColorandPoint, and other non-component classes. You can import them using one import statement each. Or, if you're sure that you've replaced all the AWT components with Swing components, import the entirejava.awtpackage again.
- Locate any source incompatibilities between the AWT component and its Swing replacement. These typically show up as compiler errors about undefined methods. For example, although AWT text components have an
addTextListenermethod, Swing text components don't. The compiler generates an error message like the following one indicating thatJTextFielddoesn't have such a method.TextEventDemo.java:22: Method addTextListener(TextEventDemo. MyTextListener) not found in class javax.swing.JTextField. textField.addTextListener(new MyTextListener("Text Field")); ^- Remove uses of deprecated API. If the compiler issues a deprecation warning, recompile with the
-deprecationflag to get details. You can find information about what deprecation means, as well as a list of alternatives to API deprecated in JDK 1.1, in Migrating to 1.1. Also, the API documentation for the deprecated class or method should have information about what to use instead.
Run the program, as described in Compiling and Running Swing Programs. If you forgot to modify any calls to
addorsetLayout, the runtime system displays a message like this:Go back to the source, fix the offending occurrence ofjava.lang.Error: Do not use javax.swing.JFrame.add() use javax.swing.JFrame.getContentPane().add() instead at javax.swing.JFrame.createRootPaneException(JFrame.java:333) at javax.swing.JFrame.addImpl(JFrame.java:355) at java.awt.Container.add(Container.java:212) at AppletDemo.main(AppletDemo.java:121)addorsetLayout, then compile and run again.
Although you might want the AWT and Swing versions of your program to be similar, be open to improvements offered by Swing. One difference you'll probably notice is that, unless your copy of the JDK has aswing.propertiesfile that specifies otherwise, your converted program uses a new look and feel: the Java Look & Feel. You can specify another look and feel if you like. For more information, see Choosing the Look and Feel.
You might be able to improve your program's GUI by using features available only to Swing components or by using the Swing components that have no AWT equivalents. Components completely new to Swing include color choosers, editable combo boxes, progress bars, split panes, tabbed panes, tables, tool tips, and trees. Features available only to Swing components include borders, which are especially useful for panels, and icons, which you can add to many components such as labels and buttons. You might also be able to replace a component you wrote yourself with a standard or customized Swing component.
Now is the time to clean up your code. If you hacked any code together to get around some of AWT's deficiencies or bugs, clean it up now! Go through the tutorial sections and examples that are relevant to your program, and make sure that you use Swing features correctly. Review your program's accessibility to make sure that your program does all it can to support assistive technologies.
|
|
Start of Tutorial > Start of Trail > Start of Lesson | Lesson Contents |