Put your user interface on a diet
Replace those heavyweight components with leaner, meaner lightweight components
By Todd Sundsted, JavaWorld.com, 03/01/98
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
A year or so ago, Java programmers building user interfaces (UI) had to live within the narrow confines of the Abstract Windowing
Toolkit (AWT). Consequently, user interface designers had to contend with a paucity of user interface components, wide variations
in their appearance and behavior across platforms, and a host of other undesirable features. It was an unpleasant time. Then,
around the middle of last year, came the first release of Swing. Swing offered many more components, a common appearance,
and identical behavior across platforms. It was a significant addition to the Java class library.
One of the key factors contributing to Swing's importance is that each user interface component in the Swing set is a lightweight component.
The user interface components supplied with early versions of Java -- which are still present in the java.awt package -- are
called heavyweight components. They are called heavyweight components because a native user interface component is used to display each Java
component -- a technique that gives Java applications the same look and feel as other applications written for a particular
platform. These components are considered heavy because they require twice as many classes to implement -- a Java class plus
its associated native class. They also have the unfortunate side effect of being opaque, which means that they can't be used
to implement components with transparent regions, or components of non-rectangular shapes.
Lightweight components, on the other hand, have no native twin -- they are free to implement their own look and feel. Consequently,
lightweight components were used as the basis for the Swing components in the JFC.
To better understand the technology behind the Swing set of user interface components and to help you see just how powerful
lightweight components are, we'll build a lightweight component of our own. Because buttons are ubiquitous user interface
components and are easy to understand in terms of their behavior, we'll implement a circular button. I'll assume you know
how the Java UI model works as defined by the AWT and that you understand the new Java 1.1 event model.
Now, let's go to work!
Building a simple lightweight button component
We begin by creating a subclass of class java.awt.Component. Both heavyweight components and lightweight components share this common ancestor. By default, subclasses of class Component aren't associated with a native component, so they are "light" by default.
Because lightweight components lack an associated native class, they have no visual representation. Therefore, they must handle
their own presentation, which they do by redefining their paint() method.
The following paint() method draws a circular button with a label. The label value (in _strLable) is set in the constructor.
public void paint(Graphics g)
{
Dimension dim = getSize();
int n = (dim.width < dim.height ? dim.width : dim.height) - 1;
// fill the interior of the button
g.setColor(getBackground());
g.fillArc(6, 6, n - 12, n - 12, 0, 360);
// draw a thin border around the button's interior
g.setColor(getBackground().darker().darker().darker().darker());
g.drawArc(6, 6, n - 12, n - 12, 0, 360);
// draw a thin ring around the entire button
g.drawArc(0, 0, n, n, 0, 360);
// draw the button's label
Font font = getFont();
if (font != null)
{
FontMetrics fontmetrics = getFontMetrics(font);
g.setColor(getForeground());
g.drawString(_strLabel,
n/2 - fontmetrics.stringWidth(_strLabel)/2,
n/2 + fontmetrics.getMaxDescent());
}
Here's what the circular buttons look like so far. Go ahead and try them out.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- java.awt.Component http://www.javasoft.com/products/jdk/1.1/docs/api/java.awt.Component.html
- Java AWTLightweight UI Framework http://www.javasoft.com/products/jdk/1.1/docs/guide/awt/designspec/lightweights.html
- JavaSoft's tutorial for creating lightweight components http://java.sun.com/products/jdk/1.1/docs/guide/awt/demos/lightweight/index.html
- Download this article and the complete source as a gzipped tar file /javaworld/jw-03-1998/howto/jw-03-howto.tar.gz
- Download this article and the complete source as a zip file /javaworld/jw-03-1998/howto/jw-03-howto.zip
- Previous How-To Java articles
- "Internationalize dynamic messages" Build flexible formatters for international applications with Java 1.1.
- "Localize this!" Use resource bundles to make your applications multicultural.
- "Write world-class applications" From Chicago to Copenhagen, Colombia to Cameroon -- Java provides the tools for writing truly international applications.
- "Use the two "R"s of Java 1.1 -- Readers and Writers" Learn how to use the two new additions to the
java.io package -- class Reader and class Writer -- to filter out unwanted e-mail.
- "Waging war on electronic junk mail" Put Java on the front line in the war against electronic junk mail.
- "Build dynamically extensible applications" Find out how to build programs that you can extend dynamically -- even while they execute.
- "3D computer graphicsGetting the hang of VRML" Learn about VRML and how you can use it to define your own virtual world.
- "3D computer graphicsMoving from wire-frame drawings to solid, shaded models" Find out how to create surfaces and add illumination to make your models more realistic.
- "3D computer graphicsSlide it, spin it, make it move -- transforming your virtual world" Learn how to make your virtual world satisfy even the toughest customer.
- "3D computer graphicsModel your world" From its start as an exotic research topic in government and university labs, virtual reality is making its move into the
mainstream of corporate America -- find out how you can gain entry into this elite club.
- "When static images just don't make the cut" Learn how to spice up your applets and applications with animated images.
- "How Java uses the the producer/consumer model to handle images -- An insider's look" Learn more about Java's powerful image-handling technique, then follow my simple procedures for building your own producer
and consumer components.
- "Learn how applets load network-based images asynchronously" Here's a close look at the way Java applets handle images in a network-based environment.
- "Drawing text is easy with three Java classes" Find out how to create text that's visually appealing through this explanation of what classes to use and how they work
together.
- "Examining HotSpot, an object-oriented drawing program" Learn how the pieces of the Java language and class library fit together through a study of this Java program
- "Using the Graphics class" A close look at the Graphics class and the drawing primitives it provides, and a demonstration of its use.
- "Observer and Observable" An introduction to the Observer interface and Observable class using the Model/View/Controller architecture as a guide.
- "The effective user interface" Five ways to enhance the appearance and effectiveness of your user interface
- "Java and event handling" How events are passed to user interface components, how to create event handlers, and more.
- "Introduction to the AWT" A description of Java's user interface toolkit.