Java Reference
In-Depth Information
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.
Search WWH ::




Custom Search