The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Writing Applets
Lesson: Overview of Applets

## Methods for Adding UI Components

> The `Simple` applet's display code (implemented in its `paint` method) is flawed: It doesn't support scrolling. Once the text it displays reaches the end of the display rectangle, you can't see any new text. Here's an example of the problem:

> This figure has been reduced to fit on the page.
> Click the image to view it at its natural size.

> The simplest cure for this problem is to use a pre-made user interface (UI) component that has the right behavior.

> Note: This page glosses over many details. To really learn about using UI components, go to Creating a User Interface (AWT Only).

### Pre-Made UI Components

> The AWT supplies the following UI components (the class that implements each component is listed in parentheses):
> - Buttons (`java.awt.Button`)
> - Checkboxes (`java.awt.Checkbox`)
> - Single-line text fields (`java.awt.TextField`)
> - Larger text display and editing areas (`java.awt.TextArea`)
> - Labels (`java.awt.Label`)
> - Lists (`java.awt.List`)
> - Pop-up lists of choices (`java.awt.Choice`)
> - Sliders and scrollbars (`java.awt.Scrollbar`)
> - Drawing areas (`java.awt.Canvas`)
> - Menus (`java.awt.Menu`, `java.awt.MenuItem`, `java.awt.CheckboxMenuItem`)
> - Containers (`java.awt.Panel`, `java.awt.Window` and its subclasses)

### Methods for Using UI Components in Applets

> Because the `Applet` class inherits from the AWT `Container` class, it's easy to add components to applets and to use layout managers to control the components' onscreen positions. Here are some of the `Container` methods an applet can use:
> `add`
>   Adds the specified `Component`.
> `remove`
>   Removes the specified `Component`.
> `setLayout`
>   Sets the layout manager.

### Adding a Non-Editable Text Field to the `Simple` Applet

> To make the `Simple` applet use a scrolling, non-editable text field, we can use the `TextField` class. Here is the revised `source code`. The changes are shown below.

> >     //Importing java.awt.Graphics is no longer necessary
> >     //since this applet no longer implements the paint method.
> >     . . .
> >     import java.awt.TextField;

> >         public class ScrollingSimple extends Applet {

> >         //Instead of using a StringBuffer, use a TextField:
> >         TextField field;

> >         public void init() {
> >             //Create the text field and make it uneditable.
> >             field = new TextField();
> >             field.setEditable(false);

> >     	//Set the layout manager so that the text field will be
> >     	//as wide as possible.
> >             setLayout(new java.awt.GridLayout(1,0));

> >     	//Add the text field to the applet.
> >             add(field);
> >             validate();

> >             addItem("initializing... ");
> >         }

> >         . . .

> >         void addItem(String newWord) {
> >             //This used to append the string to the StringBuffer;
> >             //now it appends it to the TextField.
> >             String t = field.getText();
> >             System.out.println(newWord);
> >             field.setText(t + newWord);
> >             repaint();
> >         }

> >         //The paint method is no longer necessary,
> >         //since the TextField repaints itself automatically.

> The revised `init` method creates an uneditable text field (a `TextField` instance). It sets the applet's layout manager to one that makes the text field as wide as possible (you'll learn about layout managers in Laying Out Components within a Container) and then adds the text field to the applet.

> After all this, the `init` method calls the `validate` method (which `Applet` inherits from `Component`). Invoking `validate` once after adding one or more `Component`s to an applet is a bit of voodoo that ensures that the `Component`s draw themselves onscreen. If you want to delve into the arcane reasons why `validate` works, read Details of the Component Architecture.

> Below is the resulting applet.

> > You can't run applets. Here's what you'd see if you could:

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form