Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Converting to Swing

General Conversion Tips

Empty Space

At least with the default look and feel (Java Look & Feel), the Swing components tend to be tighter (that is, they contain less empty space). To get a sparser look, you might have to add empty borders to some components or margins to text components. Another alternative is to programmatically set the look and feel to one you like. See How to Set the Look and Feel(in the Creating a User Interface trail).

Converting Painting Code

AWT components performed drawing in the paint method, and sometimes the update method. In contrast, Swing components should do all of their drawing in the paintComponent method. If you implement paintComponent then you must call super.paintComponent first thing. If you don't, even if the component is opaque it won't automatically paint its background. See Overview of Custom Painting(in the Creating a User Interface trail) for details. Most Swing components should not override or implement the paint method and no Swing component should override or implement the update method.

Note that Swing components automatically use double-buffering to increase performance. If the program that you're converting implements double-buffering explicitly, this is a unique opportunity to delete that code! For example this AWT animation program, ImageSequence.java, draws to an off-screen image then paints the image to the screen all at once. Its Swing counterpart, ImageSequenceTimer.java, simply paints directly to the custom component, which handles double buffering automatically. During the conversion process, we removed offImage and offGraphics and code that referred to them.

If your painting code draws a title or draws edges around the component, consider replacing it with a border(in the Creating a User Interface trail). For example, you an easily create a box around a group of components by adding the components to a JPanel and making the panel have a border. The AWT program CoordinatesDemo uses a class called FramedArea that exists solely to put a frame around the coordinate area. The Swing version of this program, CoordinatesDemo, uses a border instead and deletes the FramedArea class.

Converting Images

[PENDING: image icon basically wraps an Image. So why use image icons instead of images? Because Swing components are decorated with icons not images. ImageIcon class manages the tracking relationship when loading the image from its source. You can specify a description for an icon.]

Thread Issues

[PENDING: Consider using a Timer instead of a Thread.]


Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Converting to Swing