Previous | Next | Trail Map | Creating a User Interface (with Swing) | Overview of the Java UI

AWT Components


Note: We recommend that instead of using the AWT components shown on this page, you use the Swing components -- a new set of components that are part of the Java Foundation Classes (JFC). The Swing components can be used with either JDK 1.1 or JDK 1.2. They're distributed both as an integral part of JDK 1.2 and as part of JFC 1.1, which is an add-on to JDK 1.1. See Using the JFC/Swing Packages(in the Creating a User Interface trail) for details.
The applet on this page shows you the graphical UI (GUI) components the AWT provides. With the exception of menus, every GUI component is implemented with a subclass of the AWT Component(in the API reference documentation) class.


Your browser doesn't understand the <APPLET> tag. Here's a picture of the window you'd see if you were using a Java-compatible browser:



Note: Because some old browsers don't support 1.1, the above applet is a 1.0 version (here is the 1.0 code; here's the 1.1 code). To run the 1.1 version of the applet, go to example-1dot1/GUIWindow.html. For more information about running applets, refer to About Our Examples.

Implementation Note: The applet is implemented as a button that brings up the window showing the components. The window is necessary because the program includes a menu, and menus can be used only in windows. The program has a main() method so it can run as an application. The AppletButton class provides an applet framework for the window. AppletButton is a highly configurable applet that's discussed on the following pages: Deciding Which Parameters to Support(in the Writing Applets trail) and Writing the Code to Support Parameters(in the Writing Applets trail).

The Basic Controls: Buttons, Checkboxes, Choices, Lists, Menus, and Text Fields

The Button(in the Creating a User Interface trail), Checkbox(in the Creating a User Interface trail), Choice(in the Creating a User Interface trail), List(in the Creating a User Interface trail), MenuItem(in the Creating a User Interface trail), and TextField(in the Creating a User Interface trail) classes provide basic controls. These are the most common ways that users give instructions to Java programs. When a user activates one of these controls -- by clicking a button or by pressing Return in a text field, for example -- it posts an event (ACTION_EVENT). An object that contains the control can react to the event by implementing the action() method.

Other Ways of Getting User Input: Sliders, Scrollbars, and Text Areas

When the basic controls aren't appropriate, you can use the Scrollbar(in the Creating a User Interface trail) and TextArea(in the Creating a User Interface trail) classes to get user input. The Scrollbar class is used for both slider and scrollbar functionality. You can see an example of sliders in The Anatomy of a GUI-Based Program. Scrollbars are automatically included in lists and text areas (as shown in the example program) and in ScrollPane(in the Creating a User Interface trail) objects.

The TextArea class simply provides an area to display or allow editing of several lines of text. As you can see from the applet on this page, text areas automatically include scrollbars.

Creating Custom Components: Canvases

The Canvas(in the Creating a User Interface trail) class lets you write custom Components. With your Canvas subclass, you can draw custom graphics to the screen -- in a paint program, image processor, or game, for example -- and implement any kind of event handling.

Labels

A Label(in the Creating a User Interface trail) simply displays an unselectable line of text.

Containers: Windows, Panels, and Scroll Panes

The AWT provides three types of containers, all implemented as subclasses of the Container(in the API reference documentation) class (which is a Component subclass). The Window subclasses -- Dialog(in the Creating a User Interface trail), FileDialog(in the Creating a User Interface trail), and Frame(in the Creating a User Interface trail) -- provide windows to contain components. A Panel(in the Creating a User Interface trail) groups components within an area of an existing window. A ScrollPane(in the Creating a User Interface trail) is similar to a panel, but its purpose is more specialized: to display a potentially large component in a limited amount of space, generally using scrollbars to control which part of the component is displayed.

Frames create normal, full-fledged windows, as opposed to the windows that Dialogs create, which are dependent on Frames and can be modal. When you select the "File dialog..." item in the menu, the program creates a FileDialog object, which is a Dialog that can be either an Open or a Save dialog.

Here is a picture of the FileDialog window that the Solaris Applet Viewer brings up:

The example program at the beginning of this section uses a Panel to group the label and the text area, another Panel to group them with a canvas, and a third Panel to group the text field, button, checkbox, and pop-up list of choices. All these Panels are grouped by a Frame object, which presents the window they're displayed in. The Frame also holds a menu and a list.

Summary

This page presented a whirlwind tour of the AWT components. Every component this page mentions is described in detail in Using Components, the GUI Building Blocks(in the Creating a User Interface trail).


Previous | Next | Trail Map | Creating a User Interface (with Swing) | Overview of the Java UI