Main Page

Previous Next

Using Containers

A container is any component with the Container class as a base; so all the Swing components are containers. The Container class is the direct base class for the Window class and it provides the ability to contain other components. Since the Container class is an abstract class, you cannot create instances of Container. Instead it is objects of the subclasses such as Window, JFrame, or JDialog that inherit the ability to contain other components.

Important 

Note that a container cannot contain an object of the class Window, or an object of any of the classes derived from Window. An object of any other class that is derived from Component can be contained.

The components within a container are displayed within the area occupied by the container on the display screen. A dialog box, for example, might contain a JList object offering some choices: JCheckbox objects offering other options and JButton objects representing buttons enabling the user to end the dialog or enter the selections – all these components would appear within the boundaries of the dialog box. Of course, for the contained components to be visible the container must itself be displayed, as the container effectively 'owns' its components. The container also controls how its embedded components are laid out by means of a layout manager.

Before we look at what a layout manager is, and how the layout of the components in a container is determined, let's look into the basic methods defined in the Container class, and therefore available to all containers.

You can find out about the components in a container object by using the following methods defined in the Container class:

Method

Description

int getComponentCount()

Returns a count of the number of components contained by the current component.

Component getComponent (int index)

Returns the component identified by the index value. The index value is an array index so it must be between 0 and one less than the number of components contained, otherwise an ArrayIndexOutOfBoundsException will be thrown.

Component[]getComponents()

Returns an array of all the components in the current container.

If we have a Container object, content, perhaps the content pane of a JFrame window, we could iterate through the components in the Container with the following statements:

Component aComponent = null;                       // Stores a Component
int numComponents = content.getComponentCount();   // Get the count

for(int i = 0; i < numComponents; i++) 
  aComponent = content.getComponent(i);            // Get each component
  // Do something with it...
}

This retrieves the components in content one at a time in the for loop. Alternatively we could retrieve them all at once:

Component[] theComponents = content.getComponents(); // Get all components

for(int i = 0; i < theComponents.length; i++) {
  // Do something with theComponents[i]...
}

Adding Components to a Container

The components stored in a container are recorded in an array within the Container object. The array is increased in size when necessary to accommodate as many components as are present. To add a component to a container you use the method add(). The Container class defines the following four overloaded versions of the add() method:

add() Method

Description

Component add(Component c)

Add the component c to the end of the list of components stored in the container. The return value is c.

Component add(Component c, int index)

Adds the component c to the list of components in the container at the position specified by index. If index is -1, the component is added to the end of the list. If the value of index is not -1 it must be less than the number of components in the container, and greater than or equal to 0. The return value is c.

void add(Component c, Object constraints)

Add the component c to the end of the list of components stored in the container. The position of the component relative to the container is subject to the constraints defined by the second parameter. We will see what constraints are in the next section.

void add(Component c, Object constraints, int index)

Adds the component c to the list of components in the container at the position specified by index, and position subject to constraints. If index is -1, the component is added to the end of the list. If the value of index is not -1 it must be less than the number of components in the container, and greater than or equal to 0.

Note that adding a component does not displace any components already in the container. When you add a component at a given position, other components are moved in the sequence to make room for the new one. However, a component can only be in one container at a time. Adding a component to a container that is already in another container will remove it from the original container.

In order to try adding components to a container we need to understand what the constraints are that appear in some of the add() methods, and look at how the layout of components in a container is controlled.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor