Previous | Next | Trail Map | Creating a User Interface | Overview of the Java UI


AWT Components

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:


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. Here, for the curious, is the source code for the window that displays the components. 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. You can see scrollbars in the list and text areas of the applet on this page.

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 and Panels

The AWT provides two types of containers, both 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. Frames create normal, full-fledged windows, as opposed to the windows that Dialogs create, which are dependent on Frames and can be modal. Panels group components within an area of an existing window.

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 and a list.

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.

Browser Note: Netscape Navigator 2.0 doesn't implement the FileDialog class, since it never allows reading or writing of files on the local file system. Instead of seeing a file dialog, you'll see an error message in the Java Console.

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

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 | Overview of the Java UI