APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class GroupLayout

extends Object
implements LayoutManager2
Class Inheritance
All Implemented Interfaces
java.awt.LayoutManager2, java.awt.LayoutManager
Imports
java.awt.Component, .Container, .Dimension, .Insets, .LayoutManager2, java.util.*

GroupLayout is a LayoutManager that hierarchically groups components in order to position them in a Container. GroupLayout is intended for use by builders, but may be hand-coded as well. Grouping is done by instances of the Group class. GroupLayout supports two types of groups. A sequential group positions its child elements sequentially, one after another. A parallel group aligns its child elements in one of four ways.

Each group may contain any number of elements, where an element is a Group, Component, or gap. A gap can be thought of as an invisible component with a minimum, preferred and maximum size. In addition GroupLayout supports a preferred gap, whose value comes from LayoutStyle.

Elements are similar to a spring. Each element has a range as specified by a minimum, preferred and maximum. Gaps have either a developer-specified range, or a range determined by LayoutStyle. The range for Components is determined from the Component's getMinimumSize, getPreferredSize and getMaximumSize methods. In addition, when adding Components you may specify a particular range to use instead of that from the component. The range for a Group is determined by the type of group. A ParallelGroup's range is the maximum of the ranges of its elements. A SequentialGroup's range is the sum of the ranges of its elements.

GroupLayout treats each axis independently. That is, there is a group representing the horizontal axis, and a group representing the vertical axis. The horizontal group is responsible for determining the minimum, preferred and maximum size along the horizontal axis as well as setting the x and width of the components contained in it. The vertical group is responsible for determining the minimum, preferred and maximum size along the vertical axis as well as setting the y and height of the components contained in it. Each Component must exist in both a horizontal and vertical group, otherwise an IllegalStateException is thrown during layout, or when the minimum, preferred or maximum size is requested.

The following diagram shows a sequential group along the horizontal axis. The sequential group contains three components. A parallel group was used along the vertical axis.

Sequential group along the horizontal axis in three components

To reinforce that each axis is treated independently the diagram shows the range of each group and element along each axis. The range of each component has been projected onto the axes, and the groups are rendered in blue (horizontal) and red (vertical). For readability there is a gap between each of the elements in the sequential group.

The sequential group along the horizontal axis is rendered as a solid blue line. Notice the sequential group is the sum of the children elements it contains.

Along the vertical axis the parallel group is the maximum of the height of each of the components. As all three components have the same height, the parallel group has the same height.

The following diagram shows the same three components, but with the parallel group along the horizontal axis and the sequential group along the vertical axis.

Sequential group along the vertical axis in three components

As c1 is the largest of the three components, the parallel group is sized to c1. As c2 and c3 are smaller than c1 they are aligned based on the alignment specified for the component (if specified) or the default alignment of the parallel group. In the diagram c2 and c3 were created with an alignment of LEADING. If the component orientation were right-to-left then c2 and c3 would be positioned on the opposite side.

The following diagram shows a sequential group along both the horizontal and vertical axis.

Sequential group along both the horizontal and vertical axis in three components

GroupLayout provides the ability to insert gaps between Components. The size of the gap is determined by an instance of LayoutStyle. This may be turned on using the setAutoCreateGaps method. Similarly, you may use the setAutoCreateContainerGaps method to insert gaps between components that touch the edge of the parent container and the container.

The following builds a panel consisting of two labels in one column, followed by two textfields in the next column:

  JComponent panel = ...;
  GroupLayout layout = new GroupLayout(panel);
  panel.setLayout(layout);

  // Turn on automatically adding gaps between components
  layout.setAutoCreateGaps(true);

  // Turn on automatically creating gaps between components that touch
  // the edge of the container and the container.
  layout.setAutoCreateContainerGaps(true);

  // Create a sequential group for the horizontal axis.

  GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();

  // The sequential group in turn contains two parallel groups.
  // One parallel group contains the labels, the other the text fields.
  // Putting the labels in a parallel group along the horizontal axis
  // positions them at the same x location.
  //
  // Variable indentation is used to reinforce the level of grouping.
  hGroup.addGroup(layout.createParallelGroup().
           addComponent(label1).addComponent(label2));
  hGroup.addGroup(layout.createParallelGroup().
           addComponent(tf1).addComponent(tf2));
  layout.setHorizontalGroup(hGroup);

  // Create a sequential group for the vertical axis.
  GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

  // The sequential group contains two parallel groups that align
  // the contents along the baseline. The first parallel group contains
  // the first label and text field, and the second parallel group contains
  // the second label and text field. By using a sequential group
  // the labels and text fields are positioned vertically after one another.
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
           addComponent(label1).addComponent(tf1));
  vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
           addComponent(label2).addComponent(tf2));
  layout.setVerticalGroup(vGroup);

When run the following is produced.

Produced horizontal/vertical form

This layout consists of the following.

There are a couple of things to notice in this code:
Authors
Tomas Pavek, Jan Stola, Scott Violet
Since
1.6

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
GroupLayout.Alignment

Enumeration of the possible ways ParallelGroup can align its children.

private static class
GroupLayout.AutoPreferredGapMatch

Represents two springs that should have autopadding inserted between them.

private class
GroupLayout.AutoPreferredGapSpring

Spring reprensenting the distance between any number of sources and targets.

private class
GroupLayout.BaselineGroup

An extension of ParallelGroup that aligns its constituent Springs along the baseline.

private class
GroupLayout.ComponentInfo

Tracks the horizontal/vertical Springs for a Component.

private class
private class
GroupLayout.ContainerAutoPreferredGapSpring

An extension of AutopaddingSpring used for container level padding.

private static class
GroupLayout.GapSpring

Spring represented a certain amount of space.

public abstract class
GroupLayout.Group

Group provides the basis for the two types of operations supported by GroupLayout: laying out components one after another (SequentialGroup) or aligned (ParallelGroup).

private static class
public class
GroupLayout.ParallelGroup

A Group that aligns and sizes its children.

private class
GroupLayout.PreferredGapSpring

Spring representing the preferred distance between two components.

public class
GroupLayout.SequentialGroup

A Group that positions and sizes its elements sequentially, one after another.

private abstract static class
GroupLayout.Spring

Spring consists of a range: min, pref and max, a value some where in the middle of that, and a location.

private static class
GroupLayout.SpringDelta

Used by SequentialGroup in calculating resizability of springs.

Field Summary

Modifier and TypeField and Description
private boolean
private boolean
private Map<Component, GroupLayout.ComponentInfo>
public static final int
DEFAULT_SIZE

Indicates the size from the component or gap should be used for a particular range value.

private boolean
private boolean
honorsVisibility

If true, components that are not visible are treated as though they aren't there.

private GroupLayout.Group
horizontalGroup

Group responsible for layout along the horizontal axis.

private Container
private static final int
private boolean
private LayoutStyle
layoutStyle

The LayoutStyle instance to use, if null the sharedInstance is used.

private static final int
private static final int
private static final int
public static final int
PREFERRED_SIZE

Indicates the preferred size from the component or gap should be used for a particular range value.

private static final int
private boolean
private Set<GroupLayout.Spring>
private static final int
private GroupLayout.Group
verticalGroup

Group responsible for layout along the vertical axis.

Constructor Summary

AccessConstructor and Description
public
GroupLayout(Container
the Container the GroupLayout is the LayoutManager for
host
)

Creates a GroupLayout for the specified Container.

Method Summary

Modifier and TypeMethod and Description
public void
addLayoutComponent(String
the string to be associated with the component
name
,
Component
the Component to be added
component
)

Implements java.awt.LayoutManager.addLayoutComponent.

Notification that a Component has been added to the parent container.

public void
addLayoutComponent(Component
the component added
component
,
Object
description of where to place the component
constraints
)

Implements java.awt.LayoutManager2.addLayoutComponent.

Notification that a Component has been added to the parent container.

private Dimension
adjustSize(int width, int height)

private boolean
areParallelSiblings(Component source, Component target, int axis)

Returns true if the two Components have a common ParallelGroup ancestor along the particular axis.

private void
calculateAutopadding(GroupLayout.Group group, int axis, int sizeType, int origin, int size)

private void
private static void
checkLessThan(int min, int max)

private void
private static void
checkResizeType(int type, boolean isComponentSpring)

private static void
checkSize(int min, int pref, int max, boolean isComponentSpring)

public GroupLayout.ParallelGroup

Returns:

the ParallelGroup
createBaselineGroup
(boolean
whether the group is resizable
resizable
,
boolean
whether the baseline is anchored to the top or bottom of the group
anchorBaselineToTop
)

Creates and returns a ParallelGroup that aligns its elements along the baseline.

public GroupLayout.ParallelGroup

Returns:

a new ParallelGroup
createParallelGroup
()

Creates and returns a ParallelGroup with an alignment of Alignment.LEADING.

public GroupLayout.ParallelGroup

Returns:

a new ParallelGroup
createParallelGroup
(GroupLayout.Alignment
the alignment for the elements of the group
alignment
)

Creates and returns a ParallelGroup with the specified alignment.

public GroupLayout.ParallelGroup

Returns:

a new ParallelGroup
createParallelGroup
(GroupLayout.Alignment
the alignment for the elements of the group
alignment
,
boolean
true if the group is resizable; if the group is not resizable the preferred size is used for the minimum and maximum size of the group
resizable
)

Creates and returns a ParallelGroup with the specified alignment and resize behavior.

public GroupLayout.SequentialGroup

Returns:

a new SequentialGroup
createSequentialGroup
()

Creates and returns a SequentialGroup.

private void
private GroupLayout.Group
createTopLevelGroup(GroupLayout.Group specifiedGroup)

Wraps the user specified group in a sequential group.

public boolean

Returns:

true if gaps between the container and components that border the container are automatically created
getAutoCreateContainerGaps
()

Returns true if gaps between the container and components that border the container are automatically created.

public boolean

Returns:

true if gaps between components are automatically created
getAutoCreateGaps
()

Returns true if gaps between components are automatically created.

private GroupLayout.ComponentInfo
getComponentInfo(Component component)

Returns the ComponentInfo for the specified Component, creating one if necessary.

public boolean

Returns:

whether component visibility is considered when sizing and positioning components
getHonorsVisibility
()

Returns whether component visibility is considered when sizing and positioning components.

private GroupLayout.Group

Returns:

the Group responsible for positioning and sizing component along the horizontal axis
getHorizontalGroup
()

Returns the Group that positions and sizes components along the horizontal axis.

public float

Returns:

the alignment; this implementation returns .5
getLayoutAlignmentX
(Container
the Container hosting this LayoutManager
parent
)

Implements java.awt.LayoutManager2.getLayoutAlignmentX.

Returns the alignment along the x axis.

public float

Returns:

alignment; this implementation returns .5
getLayoutAlignmentY
(Container
the Container hosting this LayoutManager
parent
)

Implements java.awt.LayoutManager2.getLayoutAlignmentY.

Returns the alignment along the y axis.

public LayoutStyle

Returns:

the LayoutStyle used for calculating the preferred gap between components
getLayoutStyle
()

Returns the LayoutStyle used for calculating the preferred gap between components.

private LayoutStyle
private GroupLayout.Group

Returns:

the Group responsible for positioning and sizing component along the vertical axis
getVerticalGroup
()

Returns the Group that positions and sizes components along the vertical axis.

private void
insertAutopadding(boolean insert)

Adjusts the autopadding springs for the horizontal and vertical groups.

private void
public void
invalidateLayout(Container
the Container hosting this LayoutManager
parent
)

Implements java.awt.LayoutManager2.invalidateLayout.

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

private boolean
public void
layoutContainer(Container
the container to be laid out
parent
)

Implements java.awt.LayoutManager.layoutContainer.

Lays out the specified container.

public void
linkSize(Component...
the Components that are to have the same size
components
)

Forces the specified components to have the same size regardless of their preferred, minimum or maximum sizes.

public void
linkSize(int
the axis to link the size along; one of SwingConstants.HORIZONTAL or SwingConstants.VERTICAL
axis
,
Component...
the Components that are to have the same size
components
)

Forces the specified components to have the same size along the specified axis regardless of their preferred, minimum or maximum sizes.

public Dimension

Returns:

the maximum size for parent
maximumLayoutSize
(Container
the container to return the size for
parent
)

Implements java.awt.LayoutManager2.maximumLayoutSize.

Returns the maximum size for the specified container.

public Dimension

Returns:

the minimum size for parent
minimumLayoutSize
(Container
the container to return the size for
parent
)

Implements java.awt.LayoutManager.minimumLayoutSize.

Returns the minimum size for the specified container.

public Dimension

Returns:

the preferred size for parent
preferredLayoutSize
(Container
the container to return the preferred size for
parent
)

Implements java.awt.LayoutManager.preferredLayoutSize.

Returns the preferred size for the specified container.

private void
prepare(int sizeType)

private void
public void
removeLayoutComponent(Component
the component to be removed
component
)

Implements java.awt.LayoutManager.removeLayoutComponent.

Notification that a Component has been removed from the parent container.

public void
replace(Component
the component that should be removed and replaced with newComponent
existingComponent
,
Component
the component to put in existingComponent's place
newComponent
)

Replaces an existing component with a new one.

public void
setAutoCreateContainerGaps(boolean
whether a gap between the container and components that touch the border of the container should automatically be created
autoCreateContainerPadding
)

Sets whether a gap between the container and components that touch the border of the container should automatically be created.

public void
setAutoCreateGaps(boolean
whether a gap between components is automatically created
autoCreatePadding
)

Sets whether a gap between components should automatically be created.

public void
setHonorsVisibility(boolean
whether component visibility is considered when sizing and positioning components
honorsVisibility
)

Sets whether component visibility is considered when sizing and positioning components.

public void
setHonorsVisibility(Component
the component
component
,
Boolean
whether visibility of this component should be considered for sizing and positioning
honorsVisibility
)

Sets whether the component's visibility is considered for sizing and positioning.

public void
setHorizontalGroup(GroupLayout.Group
the Group that positions and sizes components along the horizontal axis
group
)

Sets the Group that positions and sizes components along the horizontal axis.

public void
setLayoutStyle(LayoutStyle
the LayoutStyle to use
layoutStyle
)

Sets the LayoutStyle used to calculate the preferred gaps between components.

public void
setVerticalGroup(GroupLayout.Group
the Group that positions and sizes components along the vertical axis
group
)

Sets the Group that positions and sizes components along the vertical axis.

public String

Returns:

a string representation of this GroupLayout
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of this GroupLayout.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait