APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.awt

public Class CardLayout

extends Object
implements LayoutManager2, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.awt.LayoutManager2, java.awt.LayoutManager
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .ObjectStreamField, .Serial, .Serializable, java.util.Enumeration, .Hashtable, .Vector

A CardLayout object is a layout manager for a container. It treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.

The ordering of cards is determined by the container's own internal ordering of its component objects. CardLayout defines a set of methods that allow an application to flip through these cards sequentially, or to show a specified card. The CardLayout#addLayoutComponent method can be used to associate a string identifier with a given card for fast random access.

Author
Arthur van Hoff
Since
1.0
See Also
java.awt.Container

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
CardLayout.Card

A pair of component and string that represents its name.

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv int
private static final ObjectStreamField[]
private static final long
serialVersionUID

Use serialVersionUID from JDK 1.4 for interoperability.

pack-priv Vector<CardLayout.Card>
pack-priv int

Constructor Summary

AccessConstructor and Description
public
CardLayout()

Creates a new card layout with gaps of size zero.

public
CardLayout(int
the horizontal gap.
hgap
,
int
the vertical gap.
vgap
)

Creates a new card layout with the specified horizontal and vertical gaps.

Method Summary

Modifier and TypeMethod and Description
public void
addLayoutComponent(Component
the component to be added.
comp
,
Object
a tag that identifies a particular card in the layout.
constraints
)

Implements java.awt.LayoutManager2.addLayoutComponent.

Adds the specified component to this card layout's internal table of names.

public void
addLayoutComponent(String name, Component comp)

Implements java.awt.LayoutManager.addLayoutComponent.

Deprecated replaced by addLayoutComponent(Component, Object).

If the layout manager uses a per-component string, adds the component comp to the layout, associating it with the string specified by name.

pack-priv void
checkLayout(Container parent)

Make sure that the Container really has a CardLayout installed.

public void
first(Container
the parent container in which to do the layout
parent
)

Flips to the first card of the container.

public int

Returns:

the horizontal gap between components.
getHgap
()

Gets the horizontal gap between components.

public float
getLayoutAlignmentX(Container parent)

Implements java.awt.LayoutManager2.getLayoutAlignmentX.

Returns the alignment along the x axis.

public float
getLayoutAlignmentY(Container parent)

Implements java.awt.LayoutManager2.getLayoutAlignmentY.

Returns the alignment along the y axis.

public int

Returns:

the vertical gap between components.
getVgap
()

Gets the vertical gap between components.

public void
invalidateLayout(Container target)

Implements java.awt.LayoutManager2.invalidateLayout.

Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.

public void
last(Container
the parent container in which to do the layout
parent
)

Flips to the last card of the container.

public void
layoutContainer(Container
the parent container in which to do the layout
parent
)

Implements java.awt.LayoutManager.layoutContainer.

Lays out the specified container using this card layout.

public Dimension
maximumLayoutSize(Container
the component which needs to be laid out
target
)

Implements java.awt.LayoutManager2.maximumLayoutSize.

Returns the maximum dimensions for this layout given the components in the specified target container.

public Dimension

Returns:

the minimum dimensions required to lay out the subcomponents of the specified container
minimumLayoutSize
(Container
the parent container in which to do the layout
parent
)

Implements java.awt.LayoutManager.minimumLayoutSize.

Calculates the minimum size for the specified panel.

public void
next(Container
the parent container in which to do the layout
parent
)

Flips to the next card of the specified container.

public Dimension

Returns:

the preferred dimensions to lay out the subcomponents of the specified container
preferredLayoutSize
(Container
the parent container in which to do the layout
parent
)

Implements java.awt.LayoutManager.preferredLayoutSize.

Determines the preferred size of the container argument using this card layout.

public void
previous(Container
the parent container in which to do the layout
parent
)

Flips to the previous card of the specified container.

private void
readObject(ObjectInputStream
the ObjectInputStream to read
s
)

Reads serializable fields from stream.

public void
removeLayoutComponent(Component
the component to be removed.
comp
)

Implements java.awt.LayoutManager.removeLayoutComponent.

Removes the specified component from the layout.

public void
setHgap(int
the horizontal gap between components.
hgap
)

Sets the horizontal gap between components.

public void
setVgap(int
the vertical gap between components.
vgap
)

Sets the vertical gap between components.

public void
show(Container
the parent container in which to do the layout
parent
,
String
the component name
name
)

Flips to the component that was added to this layout with the specified name, using addLayoutComponent.

pack-priv void
public String

Returns:

a string representation of this card layout.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of the state of this card layout.

private void
writeObject(ObjectOutputStream
the ObjectOutputStream to write
s
)

Writes serializable fields to stream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

currentCardback to summary
pack-priv int currentCard
hgapback to summary
pack-priv int hgap
serialPersistentFieldsback to summary
private static final ObjectStreamField[] serialPersistentFields
Annotations
@Serial
Serial Fields:
tab:Hashtable
deprecated, for forward compatibility only
hgap:int
the horizontal Layout gap
vgap:int
the vertical Layout gap
vector:Vector
the pairs of components and their names
currentCard:int
the index of Component currently displayed
serialVersionUIDback to summary
private static final long serialVersionUID

Use serialVersionUID from JDK 1.4 for interoperability.

Annotations
@Serial
vectorback to summary
pack-priv Vector<CardLayout.Card> vector
vgapback to summary
pack-priv int vgap

Constructor Detail

CardLayoutback to summary
public CardLayout()

Creates a new card layout with gaps of size zero.

CardLayoutback to summary
public CardLayout(int hgap, int vgap)

Creates a new card layout with the specified horizontal and vertical gaps. The horizontal gaps are placed at the left and right edges. The vertical gaps are placed at the top and bottom edges.

Parameters
hgap:int

the horizontal gap.

vgap:int

the vertical gap.

Method Detail

addLayoutComponentback to summary
public void addLayoutComponent(Component comp, Object constraints)

Implements java.awt.LayoutManager2.addLayoutComponent.

Adds the specified component to this card layout's internal table of names. The object specified by constraints must be a string. The card layout stores this string as a key-value pair that can be used for random access to a particular card. By calling the show method, an application can display the component with the specified name.

Parameters
comp:Component

the component to be added.

constraints:Object

a tag that identifies a particular card in the layout.

Exceptions
IllegalArgumentException:
if the constraint is not a string.
See Also
java.awt.CardLayout#show(java.awt.Container, java.lang.String)
addLayoutComponentback to summary
public void addLayoutComponent(String name, Component comp)

Implements java.awt.LayoutManager.addLayoutComponent.

Doc from java.awt.LayoutManager.addLayoutComponent.

Deprecated

replaced by addLayoutComponent(Component, Object).

If the layout manager uses a per-component string, adds the component comp to the layout, associating it with the string specified by name.

Annotations
@Deprecated
checkLayoutback to summary
pack-priv void checkLayout(Container parent)

Make sure that the Container really has a CardLayout installed. Otherwise havoc can ensue!

firstback to summary
public void first(Container parent)

Flips to the first card of the container.

Parameters
parent:Container

the parent container in which to do the layout

See Also
java.awt.CardLayout#last
getHgapback to summary
public int getHgap()

Gets the horizontal gap between components.

Returns:int

the horizontal gap between components.

Since
1.1
See Also
java.awt.CardLayout#setHgap(int), java.awt.CardLayout#getVgap()
getLayoutAlignmentXback to summary
public float getLayoutAlignmentX(Container parent)

Implements java.awt.LayoutManager2.getLayoutAlignmentX.

Returns the alignment along the x axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

getLayoutAlignmentYback to summary
public float getLayoutAlignmentY(Container parent)

Implements java.awt.LayoutManager2.getLayoutAlignmentY.

Returns the alignment along the y axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

getVgapback to summary
public int getVgap()

Gets the vertical gap between components.

Returns:int

the vertical gap between components.

See Also
java.awt.CardLayout#setVgap(int), java.awt.CardLayout#getHgap()
invalidateLayoutback to summary
public void invalidateLayout(Container target)

Implements java.awt.LayoutManager2.invalidateLayout.

Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.

lastback to summary
public void last(Container parent)

Flips to the last card of the container.

Parameters
parent:Container

the parent container in which to do the layout

See Also
java.awt.CardLayout#first
layoutContainerback to summary
public void layoutContainer(Container parent)

Implements java.awt.LayoutManager.layoutContainer.

Lays out the specified container using this card layout.

Each component in the parent container is reshaped to be the size of the container, minus space for surrounding insets, horizontal gaps, and vertical gaps.

Parameters
parent:Container

the parent container in which to do the layout

See Also
java.awt.Container#doLayout
maximumLayoutSizeback to summary
public Dimension maximumLayoutSize(Container target)

Implements java.awt.LayoutManager2.maximumLayoutSize.

Returns the maximum dimensions for this layout given the components in the specified target container.

Parameters
target:Container

the component which needs to be laid out

See Also
Container, minimumLayoutSize, preferredLayoutSize
minimumLayoutSizeback to summary
public Dimension minimumLayoutSize(Container parent)

Implements java.awt.LayoutManager.minimumLayoutSize.

Calculates the minimum size for the specified panel.

Parameters
parent:Container

the parent container in which to do the layout

Returns:Dimension

the minimum dimensions required to lay out the subcomponents of the specified container

See Also
java.awt.Container#doLayout, java.awt.CardLayout#preferredLayoutSize
nextback to summary
public void next(Container parent)

Flips to the next card of the specified container. If the currently visible card is the last one, this method flips to the first card in the layout.

Parameters
parent:Container

the parent container in which to do the layout

See Also
java.awt.CardLayout#previous
preferredLayoutSizeback to summary
public Dimension preferredLayoutSize(Container parent)

Implements java.awt.LayoutManager.preferredLayoutSize.

Determines the preferred size of the container argument using this card layout.

Parameters
parent:Container

the parent container in which to do the layout

Returns:Dimension

the preferred dimensions to lay out the subcomponents of the specified container

See Also
java.awt.Container#getPreferredSize, java.awt.CardLayout#minimumLayoutSize
previousback to summary
public void previous(Container parent)

Flips to the previous card of the specified container. If the currently visible card is the first one, this method flips to the last card in the layout.

Parameters
parent:Container

the parent container in which to do the layout

See Also
java.awt.CardLayout#next
readObjectback to summary
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException

Reads serializable fields from stream.

Parameters
s:ObjectInputStream

the ObjectInputStream to read

Annotations
@Serial
@SuppressWarnings:unchecked
Exceptions
ClassNotFoundException:
if the class of a serialized object could not be found
IOException:
if an I/O error occurs
removeLayoutComponentback to summary
public void removeLayoutComponent(Component comp)

Implements java.awt.LayoutManager.removeLayoutComponent.

Removes the specified component from the layout. If the card was visible on top, the next card underneath it is shown.

Parameters
comp:Component

the component to be removed.

See Also
java.awt.Container#remove(java.awt.Component), java.awt.Container#removeAll()
setHgapback to summary
public void setHgap(int hgap)

Sets the horizontal gap between components.

Parameters
hgap:int

the horizontal gap between components.

Since
1.1
See Also
java.awt.CardLayout#getHgap(), java.awt.CardLayout#setVgap(int)
setVgapback to summary
public void setVgap(int vgap)

Sets the vertical gap between components.

Parameters
vgap:int

the vertical gap between components.

Since
1.1
See Also
java.awt.CardLayout#getVgap(), java.awt.CardLayout#setHgap(int)
showback to summary
public void show(Container parent, String name)

Flips to the component that was added to this layout with the specified name, using addLayoutComponent. If no such component exists, then nothing happens.

Parameters
parent:Container

the parent container in which to do the layout

name:String

the component name

See Also
java.awt.CardLayout#addLayoutComponent(java.awt.Component, java.lang.Object)
showDefaultComponentback to summary
pack-priv void showDefaultComponent(Container parent)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a string representation of the state of this card layout.

Returns:String

a string representation of this card layout.

writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Writes serializable fields to stream.

Parameters
s:ObjectOutputStream

the ObjectOutputStream to write

Annotations
@Serial
Exceptions
IOException:
if an I/O error occurs
java.awt back to summary

pack-priv Class CardLayout.Card

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

A pair of component and string that represents its name.

Field Summary

Modifier and TypeField and Description
public Component
public String
private static final long
serialVersionUID

Use serialVersionUID from JDK 1.4 for interoperability.

Constructor Summary

AccessConstructor and Description
public
Card(String cardName, Component cardComponent)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

compback to summary
public Component comp
nameback to summary
public String name
serialVersionUIDback to summary
private static final long serialVersionUID

Use serialVersionUID from JDK 1.4 for interoperability.

Annotations
@Serial

Constructor Detail

Cardback to summary
public Card(String cardName, Component cardComponent)