Overview of the Java UI |
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 class.
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 and Writing the Code to Support Parameters.
The Basic Controls: Buttons, Checkboxes, Choices, Lists, Menus, and Text Fields
The Button, Checkbox, Choice, List, MenuItem, and TextField 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 theaction()
method.Other Ways of Getting User Input: Sliders, Scrollbars, and Text Areas
When the basic controls aren't appropriate, you can use the Scrollbar and TextArea 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 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 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 class (which is a Component subclass). The Window subclasses -- Dialog, FileDialog, and Frame -- 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.
Overview of the Java UI |