Java Reference
In-Depth Information
NOTE
A container cannot contain an object of the
Window
class, or an object of any of the
classes derived from
Window
. An object of any other class that is derived from
Component
can
be added to a container.
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 options,
JCheckbox
ob-
jects 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 because the con-
tainer effectively “owns" its components. The container also controls how its embedded components are laid
out by means of an object called a
layout manager
.
Before I introduce you to what a layout manager is and how the layout of the components in a container
is determined, let's consider the basic methods that the
Container
class defines that are available to all con-
tainers.
You can find out about the components in a
Container
object by using the following methods that are
defined in the
Container
class:
•
int getComponentCount()
: Returns a count of the number of components contained by the cur-
rent container.
•
Component getComponent(int index)
: Returns the component identified by the
index
value.
index
is an array index so it must be between 0 and one less than the number of components con-
tained; otherwise, an
ArrayIndexOutOfBoundsException
is thrown.
•
Component[] getComponents()
: Returns an array of all the components in the current container.
You can also obtain a reference to a component that contains a given point within the area occupied by
the container by calling the
getComponentAt()
method, with the
x
and
y
coordinates of the point as argu-
ments. If more than one component contains the point, a reference to the component closest to index 0 in the
container is returned.
If you have a
Container
object,
content
, perhaps the content pane of a
JFrame
window, you could iter-
ate through the components in the
Container
with the following statements:
Component component = null; // Stores a Component
int numComponents = content.getComponentCount(); // Get the count
for(int i = 0 ; i < numComponents ; ++i) {
component = content.getComponent(i); // Get each component
// Do something with component...
}
This retrieves the components in
content
one at a time in the
for
loop. Alternatively, you could retrieve
them from the container all at once:
Component[] theComponents = content.getComponents(); // Get all components
for(Component component : theComponents) {
// Do something with component...
}
The
getComponent()
method returns all the components in the container as an array of elements of type
Component
. You can then use the collection-based
for
loop to iterate over the components in the array.