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


The Component Hierarchy

The example program (see above) has several levels in its Component hierarchy. The parent of each level is a Container (which inherits from Component). Below is a figure of the hierarchy.

                          a Frame
                             |
                            ...
                             |
                        a Converter
                             |
           ----------------------------------
           |                                |
 a ConversionPanel (metricPanel)  a ConversionPanel (usaPanel)   
           |                                |
   -------------------              -------------------
   |       |         |              |       |         |
a Label    |      a Choice       a Label    |      a Choice
           |                                |
     --------------                  --------------
     |            |                  |            |
a TextField  a Scrollbar        a TextField  a Scrollbar

Explanation

At the top of the hierarchy is the window (Frame instance) that displays the program. When the example program runs as an application, the Frame is created in the program's main() method. When the example runs as an applet, the Frame is the browser or other applet viewer window.

Under the Frame is a Converter object, which inherits from Applet and thus is a Container (specifically, a Panel). Depending on what viewer the applet is displayed in, one or more Containers might be between the Converter object and the Frame at the top of the Component hierarchy.

Directly under the Converter object are two ConversionPanels. Here is the code that puts them under the Converter:

public class Converter extends Applet {
    . . .
    public void init() {
        . . . //Create metricPanel and usaPanel,
        . . . //two ConversionPanels.
        add(metricPanel);
        add(usaPanel);
    }
Each ConversionPanel has four children: a Label, a TextField, a Scrollbar, and a Choice. Here's the code that adds the children:
class ConversionPanel extends Panel {
    . . .
    ConversionPanel(Converter myController, String myTitle, Unit myUnits[]) {
        . . .
        //Add the label
        Label label = new Label(title, Label.CENTER);
        //...Set up GridBagConstraints for this Component...
        gridbag.setConstraints(label, c);
        add(label);

        //Add the text field
        textField = new TextField("0", 10);
        //...Set up GridBagConstraints for this Component...
        gridbag.setConstraints(textField, c);
        add(textField);
 
        //Add the pop-up list (Choice)
        unitChooser = new Choice();
        //...Initialize the Choice...
        //...Set up GridBagConstraints for this Component...
        gridbag.setConstraints(unitChooser, c);
        add(unitChooser);
 
        //Add the slider
        slider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 100, min, max);
        //...Set up GridBagConstraints for this Component...
        gridbag.setConstraints(slider, c);
        add(slider);
    }
The GridBagConstraints is an object that tells the GridBagLayout (the layout manager for this Panel) how to place a particular component. GridBagLayout, along with our other layout managers, is discussed in Laying Out Components within a Container.

Summary

The example program's Component hierarchy contains eight non-Container Components -- Components that present the graphical UI of the program. These are the Labels, TextFields, Choices, and Scrollbars the program displays. There might be additional Components such as window controls under the Frame.

The Component hierarchy has at least four Containers -- a Frame (window), a Converter (a custom kind of Panel), and two ConversionPanels (another custom Panel).

Note that if we add a new window -- a Dialog or Frame -- to the example applet, the new window will have its own Component hierarchy, unattached to the hierarchy this lesson presents.


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