sepbar-1.gif (3106 bytes)

Java 108 : AWT Component Overview

Written by David Reilly, May 4, 1998

sepbar-1.gif (3106 bytes)

Previous | Next

In this tutorial, we'll examine some of the AWT components that can be used to construct graphical user interfaces (GUIs) for Java. While it is certainly possible to create your own components, or to use those provided by graphical libraries such as Microsoft's AFC, Netscape's IFC, or the upcoming JFC components by Sun, most common user interfaces such as menus, buttons, lists, drop down lists, frames and dialogs are already available - provided by the java.awt package. Even more important is that these user-interface components fit in with the platform on which the applet/application is executed on. Thus a mac user will get a different button to a Windows user, but a button that performs the same functionality. Portability in Java is important, because you never know what type of machine or browser will be used to view your applets.

Adding components to applets

Applets can be used as a container for visual components, such as those provided by the java.awt package. Components are created, and then added to the applet container. The positioning of applet components is controlled by a layout manager.

Layout managers

There are several different layout managers, and you can even write your own if so inclined. The easiest layout manager is the FlowLayout, which adds components on a first-come, first-served manner. Applets that use a flow layout will allow components to flow freely from left to right, top to bottom, and can handle different applet sizes.

layout.gif (4173 bytes)

Example of FlowLayout, with different applet sizes

For more specialised layout, you could use a GridLayout manager, or a BorderLayout manager. GridLayouts allow you to place applets in a grid format, and BorderLayouts allow you to specify a general direction.

West Centre



BorderLayout directions

As an example, here is a code snippet using a BorderLayout, which creates a text area with a button to its right.

// Set layout manager to flow layout
setLayout(new BorderLayout());

// Add three components to applet
add(new TextArea(5,5), "North");
add(new Button("Ok"), "East");

AWT Components

There are a wide variety of AWT components, ranging from text labels and buttons to selection lists, windows and dialogs. A selection of more common components from the java.awt package is presented below, along with a sample applet to demonstrate each component's use.


Labels are great for providing user prompts, and for displaying information that should not be modified by users. If you want to have modifiable labels, consider using text fields instead. In almost all of the examples presented here, a status bar containing a label is used to convey information to the user.

Example source


Buttons are useful when an application must recieve specific instructions from the user before proceeding. For example, when a user must enter data, and then wants to perform a task, a button can be used to instruct the applet / application to proceed when the data entry is complete.

Example source

This demonstration requires Java


Choice components take up very little screen space, and can present the user with a series of choice from which a single selection can be made.

Example source

This demonstration requires Java


List components fulfill much the same functionality as choice components. Items can be added, and the currently selected item can be returned. However, they take up more space than a choice component. This can be useful at times, however, as  the items can be presented to the user, without he or she having to actually click on the component to see. Furthermore, multiple selections are allowable with the list component (though this feature must be enabled).

Example source

This demonstration requires Java


Textfield components are useful for obtaining short pieces of information from users. They are limited to single line input (for multiple lines, use a TextArea instead). Whenever a user types data into a text field, and hits enter or changes focus, the textfield will generate an event.

Example source

This demo requires Java


Textarea components are useful for getting a large amount of data from a user, or for displaying a large amount of data that is free to be modified. They take up more screen real estate than textfields, but can conserve quite a lot of room because they have support for scrollbars.

Example source

This demonstration requires Java


Using components in your applets is quite easy. With the right layout manager, and components, you can create great looking applets without even using a visual development tool. However, laying out components and creating graphical user interfaces manually requires some practise. If you choose not to use a visual development tool (such as Borland JBuilder, or Microsoft Visual J++), then its best to layout your user interface on paper first, and choose your layout managers/alignment.

Previous | Next

<-- Back to the Java Coffee Break