Java Reference
In-Depth Information
Adding Components to a Container
The components that you add to a container are recorded in a list within the Container object. To add a
component to a container, you use the add() method. The Container class defines the following four over-
loaded versions of the add() method:
Component add(Component c) : Adds c to the end of the list of components stored in the con-
tainer. The return value is c .
Component add(Component c, int index) : Adds 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) : Adds 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. You learn about constraints in the next section.
void add(Component c, Object constraints, int index) : Adds c to the list of components
in the container at the position specified by index and the 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 a component can be in only one container at a time. Adding a component to a container that is
already in another container removes it from the original container.
Before you can try adding components to a container, you need to understand the constraints that appear
in some of the add() methods and look at how the layout of components in a container is controlled.
CONTAINER LAYOUT MANAGERS
As I said, an object called a layoutmanager determines the way that components are arranged in a container.
All containers have a default layout manager, but you can choose a different layout manager when neces-
sary. Many layout manager classes are provided in the java.awt and javax.swing packages, so I introduce
those that you are most likely to need. It is also possible to create your own layout manager classes, but
creating layout managers is beyond the scope of this topic. The layout manager for a container determines
the position and size of all the components in the container, so you should not generally change the size and
position of such components yourself; just let the layout manager take care of it.
Because the classes that define layout managers all implement the LayoutManager interface, you can use
a variable of type LayoutManager to reference any of them. I introduce six layout manager classes in a little
more detail. The names of these classes and the basic arrangements that they provide are as follows:
FlowLayout places components in successive rows in a container, fitting as many on each row as
possible and starting on the next row as soon as a row is full. This works in much the same way as
your text processor places words on a line. Its primary use is for arranging buttons, although you
can use it with other components. It is the default layout manager for JPanel objects.
BorderLayout places components against any of the four borders of the container and in the cen-
ter. The component in the center fills the available space. This layout manager is the default for
the content pane in a JFrame , JDialog , or JApplet object.
CardLayout places components in a container one on top of the other — like a deck of cards. Only
the “top" component is visible at any one time.
Search WWH ::




Custom Search