What is a peer?

Scott Stanchfield

AWT uses native code (code specific to a single operating system) to display its components. Each implementation of the Jave Runtime Environment (JRE) must provide native implementations of Buttons, Labels, Panels, and all of the other AWT components.

Whenever you use an AWT component, if forwards all of its drawing requests to a piece of native code. This allows you to generically use an AWT Button in your application, but the Button appears as a Windows Button, a Motif Button, a Mac Button, or a real button on whatever platform you are running.

These pieces of native code are referred to as peers, as they share the responsibility for displaying a component.

Peer code includes native code to display components, as well as draw lines, determine platform specifics such as current screen size, and other native platform issues.

Note that the Swing components do use peers for two tasks. First, Swing's JFrame, JWindow, JDialog and JApplet extend their AWT counterparts. They all use peers to display a real drawing area on the screen.

Other Swing components are lightweight; they do not have peers. These components draw themselves on top of an existing JFrame, JWindow, JDialog or JApplet. However note that the drawing functions are implemented using peers, so they actually use peers as well, though not as their complete implementations.