| JavaTM 2 Platform Std. Ed. v1.6.0
java.awt
Class Container
java.lang.Object
java.awt.Component
java.awt.Container
- All Implemented Interfaces:
- ImageObserver, MenuContainer, Serializable
- Direct Known Subclasses:
- BasicSplitPaneDivider, CellRendererPane, DefaultTreeCellEditor.EditorContainer, JComponent, Panel, ScrollPane, Window
public class Container - extends Component
A generic Abstract Window Toolkit(AWT) container object is a component
that can contain other AWT components.
Components added to a container are tracked in a list. The order
of the list will define the components' front-to-back stacking order
within the container. If no index is specified when adding a
component to a container, it will be added to the end of the list
(and hence to the bottom of the stacking order).
Note: For details on the focus subsystem, see
How to Use the Focus Subsystem,
a section in The Java Tutorial, and the
Focus Specification
for more information.
- Since:
- JDK1.0
- See Also:
add(java.awt.Component, int),
getComponent(int),
LayoutManager,
Serialized Form
|
Constructor Summary |
Container()
Constructs a new Container. |
|
Method Summary |
Component |
add(Component comp)
Appends the specified component to the end of this container. |
Component |
add(Component comp,
int index)
Adds the specified component to this container at the given
position. |
void |
add(Component comp,
Object constraints)
Adds the specified component to the end of this container. |
void |
add(Component comp,
Object constraints,
int index)
Adds the specified component to this container with the specified
constraints at the specified index. |
Component |
add(String name,
Component comp)
Adds the specified component to this container. |
void |
addContainerListener(ContainerListener l)
Adds the specified container listener to receive container events
from this container. |
protected void |
addImpl(Component comp,
Object constraints,
int index)
Adds the specified component to this container at the specified
index. |
void |
addNotify()
Makes this Container displayable by connecting it to
a native screen resource. |
void |
addPropertyChangeListener(PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list. |
void |
addPropertyChangeListener(String propertyName,
PropertyChangeListener listener)
Adds a PropertyChangeListener to the listener list for a specific
property. |
void |
applyComponentOrientation(ComponentOrientation o)
Sets the ComponentOrientation property of this container
and all components contained within it. |
boolean |
areFocusTraversalKeysSet(int id)
Returns whether the Set of focus traversal keys for the given focus
traversal operation has been explicitly defined for this Container. |
int |
countComponents()
Deprecated. As of JDK version 1.1,
replaced by getComponentCount(). |
void |
deliverEvent(Event e)
Deprecated. As of JDK version 1.1,
replaced by dispatchEvent(AWTEvent e) |
void |
doLayout()
Causes this container to lay out its components. |
Component |
findComponentAt(int x,
int y)
Locates the visible child component that contains the specified
position. |
Component |
findComponentAt(Point p)
Locates the visible child component that contains the specified
point. |
float |
getAlignmentX()
Returns the alignment along the x axis. |
float |
getAlignmentY()
Returns the alignment along the y axis. |
Component |
getComponent(int n)
Gets the nth component in this container. |
Component |
getComponentAt(int x,
int y)
Locates the component that contains the x,y position. |
Component |
getComponentAt(Point p)
Gets the component that contains the specified point. |
int |
getComponentCount()
Gets the number of components in this panel. |
Component[] |
getComponents()
Gets all the components in this container. |
int |
getComponentZOrder(Component comp)
Returns the z-order index of the component inside the container. |
ContainerListener[] |
getContainerListeners()
Returns an array of all the container listeners
registered on this container. |
Set<AWTKeyStroke> |
getFocusTraversalKeys(int id)
Returns the Set of focus traversal keys for a given traversal operation
for this Container. |
FocusTraversalPolicy |
getFocusTraversalPolicy()
Returns the focus traversal policy that will manage keyboard traversal
of this Container's children, or null if this Container is not a focus
cycle root. |
Insets |
getInsets()
Determines the insets of this container, which indicate the size
of the container's border. |
LayoutManager |
getLayout()
Gets the layout manager for this container. |
|
getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered
as FooListeners
upon this Container. |
Dimension |
getMaximumSize()
Returns the maximum size of this container. |
Dimension |
getMinimumSize()
Returns the minimum size of this container. |
Point |
getMousePosition(boolean allowChildren)
Returns the position of the mouse pointer in this Container's
coordinate space if the Container is under the mouse pointer,
otherwise returns null. |
Dimension |
getPreferredSize()
Returns the preferred size of this container. |
Insets |
insets()
Deprecated. As of JDK version 1.1,
replaced by getInsets(). |
void |
invalidate()
Invalidates the container. |
boolean |
isAncestorOf(Component c)
Checks if the component is contained in the component hierarchy of
this container. |
boolean |
isFocusCycleRoot()
Returns whether this Container is the root of a focus traversal cycle. |
boolean |
isFocusCycleRoot(Container container)
Returns whether the specified Container is the focus cycle root of this
Container's focus traversal cycle. |
boolean |
isFocusTraversalPolicyProvider()
Returns whether this container provides focus traversal
policy. |
boolean |
isFocusTraversalPolicySet()
Returns whether the focus traversal policy has been explicitly set for
this Container. |
void |
layout()
Deprecated. As of JDK version 1.1,
replaced by doLayout(). |
void |
list(PrintStream out,
int indent)
Prints a listing of this container to the specified output
stream. |
void |
list(PrintWriter out,
int indent)
Prints out a list, starting at the specified indentation,
to the specified print writer. |
Component |
locate(int x,
int y)
Deprecated. As of JDK version 1.1,
replaced by getComponentAt(int, int). |
Dimension |
minimumSize()
Deprecated. As of JDK version 1.1,
replaced by getMinimumSize(). |
void |
paint(Graphics g)
Paints the container. |
void |
paintComponents(Graphics g)
Paints each of the components in this container. |
protected String |
paramString()
Returns a string representing the state of this Container. |
Dimension |
preferredSize()
Deprecated. As of JDK version 1.1,
replaced by getPreferredSize(). |
void |
print(Graphics g)
Prints the container. |
void |
printComponents(Graphics g)
Prints each of the components in this container. |
protected void |
processContainerEvent(ContainerEvent e)
Processes container events occurring on this container by
dispatching them to any registered ContainerListener objects. |
protected void |
processEvent(AWTEvent e)
Processes events on this container. |
void |
remove(Component comp)
Removes the specified component from this container. |
void |
remove(int index)
Removes the component, specified by index,
from this container. |
void |
removeAll()
Removes all the components from this container. |
void |
removeContainerListener(ContainerListener l)
Removes the specified container listener so it no longer receives
container events from this container. |
void |
removeNotify()
Makes this Container undisplayable by removing its connection
to its native screen resource. |
void |
setComponentZOrder(Component comp,
int index)
Moves the specified component to the specified z-order index in
the container. |
void |
setFocusCycleRoot(boolean focusCycleRoot)
Sets whether this Container is the root of a focus traversal cycle. |
void |
setFocusTraversalKeys(int id,
Set<? extends AWTKeyStroke> keystrokes)
Sets the focus traversal keys for a given traversal operation for this
Container. |
void |
setFocusTraversalPolicy(FocusTraversalPolicy policy)
Sets the focus traversal policy that will manage keyboard traversal of
this Container's children, if this Container is a focus cycle root. |
void |
setFocusTraversalPolicyProvider(boolean provider)
Sets whether this container will be used to provide focus
traversal policy. |
void |
setFont(Font f)
Sets the font of this container. |
void |
setLayout(LayoutManager mgr)
Sets the layout manager for this container. |
void |
transferFocusBackward()
Transfers the focus to the previous component, as though this Component
were the focus owner. |
void |
transferFocusDownCycle()
Transfers the focus down one focus traversal cycle. |
void |
update(Graphics g)
Updates the container. |
void |
validate()
Validates this container and all of its subcomponents. |
protected void |
validateTree()
Recursively descends the container tree and recomputes the
layout for any subtrees marked as needing it (those marked as
invalid). |
| Methods inherited from class java.awt.Component |
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle |
Container
public Container()
- Constructs a new Container. Containers can be extended directly,
but are lightweight in this case and must be contained by a parent
somewhere higher up in the component tree that is native.
(such as Frame for example).
getComponentCount
public int getComponentCount()
- Gets the number of components in this panel.
- Returns:
- the number of components in this panel.
- Since:
- JDK1.1
- See Also:
getComponent(int)
countComponents
@Deprecated
public int countComponents()
- Deprecated. As of JDK version 1.1,
replaced by getComponentCount().
getComponent
public Component getComponent(int n)
- Gets the nth component in this container.
- Parameters:
n - the index of the component to get.
- Returns:
- the nth component in this container.
- Throws:
ArrayIndexOutOfBoundsException - if the nth value does not exist.
getComponents
public Component[] getComponents()
- Gets all the components in this container.
- Returns:
- an array of all the components in this container.
getInsets
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.
- Returns:
- the insets of this container.
- Since:
- JDK1.1
- See Also:
Insets,
LayoutManager
insets
@Deprecated
public Insets insets()
- Deprecated. As of JDK version 1.1,
replaced by
getInsets().
add
public Component add(Component comp)
- Appends the specified component to the end of this container.
This is a convenience method for
addImpl(java.awt.Component, java.lang.Object, int).
Note: If a component has been added to a container that
has been displayed, validate must be
called on that container to display the new component.
If multiple components are being added, you can improve
efficiency by calling validate only once,
after all the components have been added.
- Parameters:
comp - the component to be added
- Returns:
- the component argument
- Throws:
NullPointerException - if comp is null- See Also:
addImpl(java.awt.Component, java.lang.Object, int),
validate(),
JComponent.revalidate()
add
public Component add(String name,
Component comp)
- Adds the specified component to this container.
This is a convenience method for
addImpl(java.awt.Component, java.lang.Object, int).
This method is obsolete as of 1.1. Please use the
method add(Component, Object) instead.
- Throws:
NullPointerException - if comp is null- See Also:
add(Component, Object)
add
public Component add(Component comp,
int index)
- Adds the specified component to this container at the given
position.
This is a convenience method for
addImpl(java.awt.Component, java.lang.Object, int).
Note: If a component has been added to a container that
has been displayed, validate must be
called on that container to display the new component.
If multiple components are being added, you can improve
efficiency by calling validate only once,
after all the components have been added.
- Parameters:
comp - the component to be addedindex - the position at which to insert the component,
or -1 to append the component to the end
- Returns:
- the component
comp
- Throws:
NullPointerException - if comp is null
IllegalArgumentException - if index is invalid (see
addImpl(java.awt.Component, java.lang.Object, int) for details)- See Also:
addImpl(java.awt.Component, java.lang.Object, int),
remove(int),
validate(),
JComponent.revalidate()
setComponentZOrder
public void setComponentZOrder(Component comp,
int index)
- Moves the specified component to the specified z-order index in
the container. The z-order determines the order that components
are painted; the component with the highest z-order paints first
and the component with the lowest z-order paints last.
Where components overlap, the component with the lower
z-order paints over the component with the higher z-order.
If the component is a child of some other container, it is
removed from that container before being added to this container.
The important difference between this method and
java.awt.Container.add(Component, int) is that this method
doesn't call removeNotify on the component while
removing it from its previous container unless necessary and when
allowed by the underlying native windowing system. This way, if the
component has the keyboard focus, it maintains the focus when
moved to the new position.
This property is guaranteed to apply only to lightweight
non-Container components.
Note: Not all platforms support changing the z-order of
heavyweight components from one container into another without
the call to removeNotify. There is no way to detect
whether a platform supports this, so developers shouldn't make
any assumptions.
- Parameters:
comp - the component to be movedindex - the position in the container's list to
insert the component, where getComponentCount()
appends to the end
- Throws:
NullPointerException - if comp is
null
IllegalArgumentException - if comp is one of the
container's parents
IllegalArgumentException - if index is not in
the range [0, getComponentCount()] for moving
between containers, or not in the range
[0, getComponentCount()-1] for moving inside
a container
IllegalArgumentException - if adding a container to itself
IllegalArgumentException - if adding a Window
to a container- Since:
- 1.5
- See Also:
getComponentZOrder(java.awt.Component)
getComponentZOrder
public int getComponentZOrder(Component comp)
- Returns the z-order index of the component inside the container.
The higher a component is in the z-order hierarchy, the lower
its index. The component with the lowest z-order index is
painted last, above all other child components.
- Parameters:
comp - the component being queried
- Returns:
- the z-order index of the component; otherwise
returns -1 if the component is
null
or doesn't belong to the container - Since:
- 1.5
- See Also:
setComponentZOrder(java.awt.Component, int)
add
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.
This is a convenience method for
addImpl(java.awt.Component, java.lang.Object, int).
Note: If a component has been added to a container that
has been displayed, validate must be
called on that container to display the new component.
If multiple components are being added, you can improve
efficiency by calling validate only once,
after all the components have been added.
- Parameters:
comp - the component to be addedconstraints - an object expressing
layout contraints for this component
- Throws:
NullPointerException - if comp is null- Since:
- JDK1.1
- See Also:
addImpl(java.awt.Component, java.lang.Object, int),
validate(),
JComponent.revalidate(),
LayoutManager
add
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.
This is a convenience method for
addImpl(java.awt.Component, java.lang.Object, int).
Note: If a component has been added to a container that
has been displayed, validate must be
called on that container to display the new component.
If multiple components are being added, you can improve
efficiency by calling validate only once,
after all the components have been added.
- Parameters:
comp - the component to be addedconstraints - an object expressing layout contraints for thisindex - the position in the container's list at which to insert
the component; -1 means insert at the end
component
- Throws:
NullPointerException - if comp is null
IllegalArgumentException - if index is invalid (see
addImpl(java.awt.Component, java.lang.Object, int) for details)- See Also:
addImpl(java.awt.Component, java.lang.Object, int),
validate(),
JComponent.revalidate(),
remove(int),
LayoutManager
addImpl
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 via the
addLayoutComponent
method.
The constraints are
defined by the particular layout manager being used. For
example, the BorderLayout class defines five
constraints: BorderLayout.NORTH,
BorderLayout.SOUTH, BorderLayout.EAST,
BorderLayout.WEST, and BorderLayout.CENTER.
The GridBagLayout class requires a
GridBagConstraints object. Failure to pass
the correct type of constraints object results in an
IllegalArgumentException.
If the current layout manager implements LayoutManager2, then
LayoutManager2.addLayoutComponent(Component,Object) is invoked on
it. If the current layout manager does not implement
LayoutManager2, and constraints is a String, then
LayoutManager.addLayoutComponent(String,Component) is invoked on it.
If the component is not an ancestor of this container and has a non-null
parent, it is removed from its current parent before it is added to this
container.
This is the method to override if a program needs to track
every add request to a container as all other add methods defer
to this one. An overriding method should
usually include a call to the superclass's version of the method:
super.addImpl(comp, constraints, index)
- Parameters:
comp - the component to be addedconstraints - an object expressing layout constraints
for this componentindex - the position in the container's list at which to
insert the component, where -1
means append to the end
- Throws:
IllegalArgumentException - if index is invalid;
if comp is a child of this container, the valid
range is [-1, getComponentCount()-1]; if component is
not a child of this container, the valid range is
[-1, getComponentCount()]
IllegalArgumentException - if comp is an ancestor of
this container
IllegalArgumentException - if adding a window to a container
NullPointerException - if comp is null- Since:
- JDK1.1
- See Also:
add(Component),
add(Component, int),
add(Component, java.lang.Object),
LayoutManager,
LayoutManager2
remove
public void remove(int index)
- Removes the component, specified by
index,
from this container.
This method also notifies the layout manager to remove the
component from this container's layout via the
removeLayoutComponent method.
Note: If a component has been removed from a container that
had been displayed, validate() must be
called on that container to reflect changes.
If multiple components are being removed, you can improve
efficiency by calling validate() only once,
after all the components have been removed.
- Parameters:
index - the index of the component to be removed
- Throws:
ArrayIndexOutOfBoundsException - if index is not in
range [0, getComponentCount()-1]- Since:
- JDK1.1
- See Also:
add(java.awt.Component),
validate(),
getComponentCount()
remove
public void remove(Component comp)
- Removes the specified component from this container.
This method also notifies the layout manager to remove the
component from this container's layout via the
removeLayoutComponent method.
Note: If a component has been removed from a container that
had been displayed, validate() must be
called on that container to reflect changes.
If multiple components are being removed, you can improve
efficiency by calling validate() only once,
after all the components have been removed.
- Parameters:
comp - the component to be removed- See Also:
add(java.awt.Component),
validate(),
remove(int)
removeAll
public void removeAll()
- Removes all the components from this container.
This method also notifies the layout manager to remove the
components from this container's layout via the
removeLayoutComponent method.
- See Also:
add(java.awt.Component),
remove(int)
getLayout
public LayoutManager getLayout()
- Gets the layout manager for this container.
- See Also:
doLayout(),
setLayout(java.awt.LayoutManager)
setLayout
public void setLayout(LayoutManager mgr)
- Sets the layout manager for this container.
- Parameters:
mgr - the specified layout manager- See Also:
doLayout(),
getLayout()
doLayout
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.
- Overrides:
doLayout in class Component
- Since:
- JDK1.1
- See Also:
LayoutManager.layoutContainer(java.awt.Container),
setLayout(java.awt.LayoutManager),
validate()
layout
@Deprecated
public void layout()
- Deprecated. As of JDK version 1.1,
replaced by
doLayout().
- Overrides:
layout in class Component
invalidate
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.
If the LayoutManager installed on this container is
an instance of LayoutManager2, then
LayoutManager2.invalidateLayout(Container) is invoked on
it supplying this Container as the argument.
- Overrides:
invalidate in class Component
- See Also:
validate(),
layout(),
LayoutManager,
LayoutManager2.invalidateLayout(Container)
validate
public void validate()
- Validates this container and all of its subcomponents.
The validate method is used to cause a container
to lay out its subcomponents again. It should be invoked when
this container's subcomponents are modified (added to or
removed from the container, or layout-related information
changed) after the container has been displayed.
If this Container is not valid, this method invokes
the validateTree method and marks this Container
as valid. Otherwise, no action is performed.
- Overrides:
validate in class Component
- See Also:
add(java.awt.Component),
Component.invalidate(),
JComponent.revalidate(),
validateTree()
validateTree
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.
- See Also:
doLayout(),
validate()
setFont
public void setFont(Font f)
- Sets the font of this container.
- Overrides:
setFont in class Component
- Parameters:
f - The font to become this container's font.- Since:
- JDK1.0
- See Also:
Component.getFont()
getPreferredSize
public Dimension getPreferredSize()
- Returns the preferred size of this container. If the preferred size has
not been set explicitly by
Component.setPreferredSize(Dimension)
and this Container has a non-null LayoutManager,
then LayoutManager.preferredLayoutSize(Container)
is used to calculate the preferred size.
Note: some implementations may cache the value returned from the
LayoutManager. Implementations that cache need not invoke
preferredLayoutSize on the LayoutManager every time
this method is invoked, rather the LayoutManager will only
be queried after the Container becomes invalid.
- Overrides:
getPreferredSize in class Component
- Returns:
- an instance of
Dimension that represents
the preferred size of this container. - See Also:
getMinimumSize(),
getMaximumSize(),
getLayout(),
LayoutManager.preferredLayoutSize(Container),
Component.getPreferredSize()
preferredSize
@Deprecated
public Dimension preferredSize()
- Deprecated. As of JDK version 1.1,
replaced by
getPreferredSize().
- Overrides:
preferredSize in class Component
getMinimumSize
public Dimension getMinimumSize()
- Returns the minimum size of this container. If the minimum size has
not been set explicitly by
Component.setMinimumSize(Dimension)
and this Container has a non-null LayoutManager,
then LayoutManager.minimumLayoutSize(Container)
is used to calculate the minimum size.
Note: some implementations may cache the value returned from the
LayoutManager. Implementations that cache need not invoke
minimumLayoutSize on the LayoutManager every time
this method is invoked, rather the LayoutManager will only
be queried after the Container becomes invalid.
- Overrides:
|