Put your user interface on a diet

Replace those heavyweight components with leaner, meaner lightweight components

1 2 Page 2
Page 2 of 2

It works pretty well, doesn't it?

The completed button

The circular button component that we've created is a first-class Java user interface component. You can add it to containers just as you would any other component. It even behaves much like the basic AWT Button class and in many cases could be used in its place.

Feel free to improve and expand on what we've done -- I did. Here's a look at my final, improved incarnation of the circular button.

You need a Java-enabled browser to see this applet.

It works exactly like the circular buttons we built, except that it handles input focus as well. Notice how the border of the circular button thickens when the button is pressed. Thickening of the border indicates that this button, out of all of the other components making up the user interface, has the input focus.

I've also made it possible to move the buttons around. Simply hold down the shift key while dragging the mouse over a pressed button and the button will move. Try to place one circular button so that it overlaps the other. You'll notice that part of the button is transparent. You couldn't achieve that effect without lightweight components.

At this point, you might want to take a close look at the code. See if you can find the code that handles the focus. Some of you might also want to take a peek at the code for the container that holds these buttons. It allows the user to move components and handles double-buffering (which improves the performance of lightweight components).

Conclusion

I hope I've given you a taste of the power of the lightweight component framework. It enables programmers to create truly useful and unique user interface components -- components you couldn't create under the old heavyweight framework. Remember, the Swing user interface toolkit itself depends heavily on the lightweight component framework. I encourage you to begin where I've left off.

And stay tuned. In the coming months I'm going to explore additional pieces of the Java Foundation Classes.

Todd Sundsted has been writing programs since computers became available in convenient desktop models. Though originally interested in building distributed object applications in C++, Todd moved on to the Java programming language when it became the obvious choice for that sort of thing. In addition to writing, Todd is president of Etcee, which offers Java-centric training, mentoring, and consulting.

Learn more about this topic

  • 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

1 2 Page 2
Page 2 of 2