The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

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

General Conversion Tips

This section gives you conversion tips for dealing with empty space, painting, and images.

Empty Space

If you use the Java Look & Feel, your Swing program might have less space between components than the previous version. If you want to add more space, you can do it using any combination of layout managers, empty borders, and invisible spacer components. See Putting Space Between Components(in the Creating a GUI with JFC/Swing trail) for more information. You can also use the setMargin method for text components.

Converting Painting Code

AWT components perform painting in the paint and update methods. Swing components use the paintComponent method instead. To take advantage of automatic background painting, your implementation of paintComponent should call super.paintComponent, first thing. See Overview of Custom Painting(in the Creating a GUI with JFC/Swing trail) for details.

Note that Swing components automatically use double buffering to make their painting smooth. If the program that you're converting implements double buffering explicitly, this is a unique opportunity to delete some code! For example the AWT-based animation program ImageSequence.java(in a .java source file) paints to an off-screen image and then paints the image to the screen all at once. Its Swing counterpart, ImageSequenceTimer.java(in a .java source file), simply puts its paint code in the custom component's paintComponent method, and double buffering is handled automatically. During the conversion process, we removed offImage, offGraphics, and all code that referred to them.

If your painting code puts a title or edges around the component, consider replacing it with a border(in the Creating a GUI with JFC/Swing trail). For example, you can 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-based program CoordinatesDemo(in a .java source file) uses a class called FramedArea that exists solely to put a frame around the coordinate area. The Swing version of this program, CoordinatesDemo(in a .java source file), uses a border instead and deletes the FramedArea class.

Converting Images

Although you can use Image objects in Swing programs, you might want to convert some or all of them into Icon objects. The main reason is Swing components that can display images -- labels, buttons, cell renderers in tables and trees and lists, tabbed panes, menus, and so on -- do so using Icon objects. Another reason is that Icon objects have accessibility support built in.

You can convert an Image into an icon in one of two ways. The first way is to use new ImageIcon(anImage). The second is to create an ImageIcon using the same data source that you used to create the Image. When you use the second way, the constructor automatically loads the image data, returning when the image is ready to use.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

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