Widgets (Components) Example Code
David Matuszek

Components (also known as widgets) are the various user controls and displays provided by the Java AWT; they are the basic building blocks of a Graphical User Interface. The following illustration shows the most common AWT components:

  • Label
  • Button
  • Checkbox
  • Choice
  • Scrollbar
  • List
  • TextField
  • TextArea
  • CheckboxGroup (containing Checkboxes)

Each of the components in the above Applet, when manipulated, places a message in the status line of the applet (in the above illustration, the status line contains the text Applet started.) The status line is visible when you run the applet with appletviewer, but is usually not shown by browsers.

The code for this applet illustrates:

  1. Constructing various components,
  2. Adding components to the Applet,
  3. Attaching listeners to each component, and
  4. Writing the listeners.

There are two versions of the source code for this applet:

Widgets.java uses anonymous inner classes; complete BlueJ package is in WidgetsAnonymous.zip.

Widgets2.java uses member classes; complete BlueJ package is in WidgetsMembers.zip.

In general, member classes are easier to understand, while anonymous inner classes are more convenient.