[Chapter 11] Using and Creating GUI Components
Author: Pat Niemeyer and Josh Peck
Date: Tue Jul 22 19:01:01 1997

#

| Chapter 11 |

# 11. Using and Creating GUI Components

Contents:
Buttons and Labels
Text Components
Lists
Menus and Choices
PopupMenus
Checkboxes and CheckboxGroups
ScrollPane and Scrollbars
Dialogs
Creating custom components

The last chapter discussed a lot of concepts: how Java's user interface facility is put together, and how the larger pieces work. You should understand what components and containers are, how you use them to create a display, what events are, how components use them to communicate with the rest of your application, and what layout managers are. In other words, we've covered a lot of material that's "good for you."

Now that we're through with the general concepts and background, we'll get to the fun stuff: how to do things with AWT. We will cover all the components that the AWT package supplies, how to use these components in applets and applications, and how to build your own components. We will have lots of code and lots of pretty examples to look at. Here's a list of the components we cover:

- `Button` and `Label`

- Text components: `TextArea` and `TextField`

- `List`

- `Menu` and `Choice`

- `PopupMenu`

- `Checkbox` and checkbox grouping

- `Scrollbar` and `ScrollPane`

- `Dialog` and `FileDialog`

When we discuss how to create your own components, we'll cover these issues:

- Extending canvas

- Lightweight components

- Making components that fire events

## 11.1 Buttons and Labels

We'll start with the simplest components, buttons, and labels. Frankly, there isn't much to say about them. If you've seen one button, you've seen them all; and you've already seen buttons in the applets of Chapter 2 (`HelloWeb3` and `HelloWeb4`). A button generates an ActionEvent when the user presses it. To receive these events, your program registers an `ActionListener`, which must implement the `actionPerformed()` method. The argument passed to `actionPerformed()` is the event itself. Rather than rehash this material, I'll refer you to Chapter 2.

So much for review. There's one more thing worth saying about buttons, which applies to any component that generates an action event. Java lets us specify an "action command" for buttons (and other components, like menu items, that can generate action events). The action command is less interesting than it sounds. It is just a String that serves to identify the component that sent the event. By default, the action command of a Button is the same as the text of its label; it is included in action events, so you can use it to figure out which button an event came from. To get the action command from an action event, call the event's `getActionCommand()` method. The following code checks whether the user pressed the "Yes" button:

    public void actionPerformed(ActionEvent e){
        if (e.getActionCommand().equals("Yes") {
            //the user pressed "Yes"; do something
            ...
        }
    }

You can change the action command by calling the button's `setActionCommand()` method. The code below changes the button `b`'s action command to "confirm":

    myButton.setActionCommand("confirm");

It's a good idea to get used to setting action commands explicitly, because they prevent your code from breaking when you or some other developer "internationalizes" it. If you rely on the button's label, your code will stop working as soon as that label changes; a French user might see the label "Oui" rather than "Yes." By setting the action command, you eliminate one source of bugs; for example, the button `myButton` above will always generate the action command "confirm," regardless of what its label says.

There's even less to be said about Label components. They're just text strings, housed in a component. There aren't any special events associated with labels; about all you can do is specify the text's alignment, which controls the position of the text within the area that the label occupies when displayed. The following code creates some labels with different alignments:

    Label l1 = new Label("Lions"); //label with default alignment (CENTER)
    Label l2 = new Label("Tigers", LEFT); //left aligned label
    Label l3 = new Label (); //label with no text, default alignment
    l3.setText("and Bears"); //assigning text to l3
    l3.setAlignment(RIGHT); //setting l3's alignment

Now we've built three labels, using all three constructors and several of the class's methods. To display the labels, you only have to add them to a container by calling the container's `add()` method.

The other characteristics you might like to set on labels, like changing their font or color, are accomplished using the methods of the Component class Chapter 10, Understand the Abstract Windowing Toolkit For example, you can call `setFont()` and `setColor()` on a label, as with any other component.

Given that labels are so simple, why do we need them at all? Why not just call `drawString()` whenever we need text? Remember that a `Label` is a `Component`. That's important; it means that labels have the normal complement of methods for setting fonts and colors that we mentioned above, as well as the ability to be managed sensibly by a layout manager. Therefore, they're much more flexible.

Speaking of layouts--if you use the setText() method to change the text of your label, its size will probably change. So you should remember to call validate() on its container, to lay things out again.[1]

> [1] At least as of Java 1.1, labels aren't very smart. Simply validating the container isn't enough. I had to explicitly invalidate the label first.

    label.setText(...);
    label.invalidate();
    validate();  // on the container holding the label

This ought to be considered a bug.

Applets |  | Text Components