The JavaTM Tutorial

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

Trail: Creating a GUI with JFC/Swing
Lesson: Converting to Swing

## Swing Replacements for AWT Components

> Use the following table as a guide for choosing a Swing replacement for each AWT component used in your program.

> AWT Component | Closest Swing Equivalent | Notes
> --- | --- | ---
> `java.applet.Applet` | `JApplet` | AWT applets and Swing applets differ in several ways. See Converting Applets.
> `Button` | `JButton` | A Swing button can include an image and/or text.
> `Canvas` | `JPanel`, `JLabel`, or another appropriate Swing component | Your choice depends on what the program uses the canvas for. See Converting Canvases for a discussion of your conversion options.
> `Checkbox` | `JCheckBox` or `JRadioButton` | Note the `B' is capitalized in the Swing class name but not in the AWT class name.
> `CheckboxMenuItem` | `JCheckBoxMenuItem` | Note the `B' is capitalized in the Swing class name but not in the AWT class name. Also, note that Swing menu components are true components.
> `Choice` | `JComboBox` | Replace a `Choice` with an uneditable (the default) `JComboBox`. You might have to re-write code that handles item events. Refer to Converting Choices.
> `Dialog` | `JDialog` or `JOptionPane` | AWT-based programs add components directly to a dialog and directly set its layout manager. In contrast, Swing-based programs add components to and set the layout manager on a `JDialog`'s content pane.
> `FileDialog` | `JFileChooser` | `FileDialog` is a dialog, while `JFileChooser` is a component that you can place in any top-level container. For convenience, `JFileChooser` provides methods that make it easy to display a file chooser in a dialog.
> `Frame` | `JFrame` | AWT-based programs add components directly to a frame and directly set its layout manager. In contrast, Swing-based programs add components to and set the layout manager on a `JFrame`'s content pane.
> `Label` | `JLabel` | A Swing label can include an image and/or text. To support accessibility, use `setLabelFor` to associate each label with the component it describes (if any).
> `List` | `JList` | AWT lists and Swing lists differ in many ways. See Converting Lists for information and examples. If you'd like your list to display multiple columns of information, consider upgrading to a table. If the list contains hierarchical informaiton, consider using a tree.
> `Menu` | `JMenu` | Swing menu components are true components.
> `MenuBar` | `JMenuBar` | Swing menu components are true components.
> `MenuItem` | `JMenuItem` | Swing menu components are true components.
> `Panel` | `JPanel` | You can easily add borders to Swing panels.
> `PopupMenu` | `JPopupMenu` | Swing menu components are true components.
> `Scrollbar` | `JScrollPane` or `JSlider` or `JProgressBar` | Although Swing has a `JScrollBar` class, you don't usually use it directly.
> `ScrollPane` | `JScrollPane` | You can add custom decorations, including row and column headers, to Swing scroll panes.
> `TextArea` | `JTextArea` | Typically requires some re-coding to convert. See Converting Text Components for information and examples.
> `TextField` | `JTextField` | For simple uses, `JTextField` is source-compatible with `TextField`. If you use text listeners you need to modify your code to use a custom document or document listener instead. If you need a password field, use `JPasswordField` instead. See Converting Text Components for information about non-trivial conversions and examples.
> `Window` | `JWindow` or `JToolTip` | You might be able to substitute a non-window component, such as a `JLabel`, and use a layered pane.

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

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.