Peer vs. Swing Components

The Label class and the other components you'll learn about today are heavyweight components based on peers. Peers are native graphical user interface widgets. For instance, when Java is asked to draw a label it doesn't just draw a string in the right place on the screen. It creates whatever the native GUI's concept of a Label component is and puts that on the screen. This means that a label looks like a Windows label on Windows; a label looks like a Mac label on the Mac; a label looks like a Motif label on Motif, and so on.

One problem with this aproach is that not all GUIs support changing the fonts, colors, and so on of their native peers. Another problem is that this approach adds an additional level of indirection that cuts into performance. An alternate approach is to implement only very basic graphics primitives like drawString() and drawLine() in native code, then draw the components like buttons and labels using those graphics primitives.

In Java 1.2 and later the javax.swing package provides a set of classes that do exactly this. The Swing classes provide parallels to many standard AWT components with different names but similar or expanded interfaces. For instance, a JLabel is the Swing equivalent of a java.awt.Label. A JButton is the Swing equivalent of a java.awt.Button. Swing also includes some additional GUI widgets that are not available in the standard AWT, a tree control for example.


Previous | Next | Top | Cafe au Lait

Copyright 1998, 1999, 2004 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified November 5, 2004