Components & Containers
Home ] Up ] FlowLayout ] LayoutManager ] LayoutManager2 ] [ Components & Containers ] BorderLayout ] GridLayout ] Insets ] Nested Layouts ]

 

 

The Component Class

Components implement two methods that affect their interactions with layout managers:

Method Description
public Dimension getPreferredSize()
Gets the preferred size of this component.
public Dimension getMinimumSize()
Gets the minimum size of this component.

Each Component within a Container tells the Container's layout manager what size it would prefer to be, and what minimum size it needs to be.   Typically, a layout manager will cycle through the Components within the Container to determine the preferred and minimum sizes for the container itself.

The Container Class

Containers implement a number of methods that affect their interactions with layout managers:

Method Description
public Component add(Component comp)
Adds the specified component to the end of this container.
public Component add(Component comp,
                     int index)
 
Adds the specified component to this container at the given position.  The index is the position at which to insert the component, or -1 to insert the component at the end.
public void add(Component comp, 
                Object constraints)
Adds the specified component to the end of this container. Also notifies the layout manager to add the component to this container's layout using the specified constraints object.
public void add(Component comp,
                Object constraints,
                int index)
Adds the specified component to this container with the specified constraints at the specified index. Also notifies the layout manager to add the component to the this container's layout using the specified constraints object.
protected void addImpl(Component comp,
                       Object constraints,
                       int index)
Adds the specified component to this container at the specified index. This method also notifies the layout manager to add the component to this container's layout using the specified constraints object.

This is the method to override if a program needs to track every add request to a container. An overriding method should usually include a call to the superclass's version of the method:

super.addImpl(comp, constraints, index)
public Component getComponent(int n)
Gets the nth component in this container.
public Component[] getComponents()
Gets all the components in this container.
public int getComponentCount()
Gets the number of components in this panel.
public Insets getInsets()
Determines the insets of this container, which indicate the size of the container's border.
A Frame object, for example, has a top inset that corresponds to the height of the frame's title bar.
public void remove(int index)
Removes the component, specified by index, from this container.
public void remove(Component comp)
Removes the specified component from this container.
public void removeAll()
Removes all the components from this container.
public LayoutManager getLayout()
Gets the layout manager for this container.
public void setLayout(LayoutManager mgr)
Sets the layout manager for this container.
public void doLayout()
Causes this container to lay out its components. Most programs should not call this method directly, but should invoke the validate method instead.
public void invalidate()
Invalidates the container. The container and all parents above it are marked as needing to be laid out. This method can be called often, so it needs to execute quickly.
public void validate()
Validates this container and all of its subcomponents.
AWT uses validate to cause a container to lay out its subcomponents again after the components it contains have been added to or modified.
protected void validateTree()
Recursively descends the container tree and recomputes the layout for any subtrees marked as needing it (those marked as invalid). Synchronization should be provided by the method that calls this one: validate.
 

This page was last modified on 02 October, 2007