GroupLayout

GroupLayout groups its components and places them in a Container hierarchically. The grouping is done by instances of the Group class.

Group is an abstract class, and two concrete classes which implement this Group class are SequentialGroup and ParallelGroup.

SequentialGroup positions its child sequentially one after another whereas ParallelGroup aligns its child on top of each other.

The GroupLayout class provides methods such as createParallelGroup() and createSequentialGroup() to create groups.

GroupLayout treats each axis independently. That is, there is a group representing the horizontal axis, and a group representing the vertical axis. 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.

Nested Classes

Modifier and TypeClassDescription
static classGroupLayout.AlignmentEnumeration of the possible ways ParallelGroup can align its children.
classGroupLayout.GroupGroup provides the basis for the two types of operations supported by GroupLayout: laying out components one after another (SequentialGroup) or aligned (ParallelGroup).
classGroupLayout.ParallelGroupIt is a Group that aligns and sizes it's children.
classGroupLayout.SequentialGroupIt is a Group that positions and sizes its elements sequentially, one after another.

Fields

Modifier and TypeFieldDescription
static intDEFAULT_SIZEIt indicates the size from the component or gap should be used for a particular range value.
static intPREFERRED_SIZEIt indicates the preferred size from the component or gap should be used for a particular range value.

Constructors

GroupLayout(Container host)It creates a GroupLayout for the specified Container.

Useful Methods

Modifier and TypeFieldDescription
voidaddLayoutComponent(Component component, Object constraints)It notify that a Component has been added to the parent container.
voidaddLayoutComponent(String name, Component component)It notify that a Component has been added to the parent container.
GroupLayout.ParallelGroupcreateBaselineGroup(boolean resizable, boolean anchorBaselineToTop)It creates and returns a ParallelGroup that aligns it's elements along the baseline.
GroupLayout.ParallelGroupcreateParallelGroup()It creates and returns a ParallelGroup with an alignment of Alignment.LEADING
GroupLayout.ParallelGroupcreateParallelGroup(GroupLayout.Alignment alignment)It creates and returns a ParallelGroup with the specified alignment.
GroupLayout.ParallelGroupcreateParallelGroup(GroupLayout.Alignment alignment, boolean resizable)It creates and returns a ParallelGroup with the specified alignment and resize behavior.
GroupLayout.SequentialGroupcreateSequentialGroup()It creates and returns a SequentialGroup.
booleangetAutoCreateContainerGaps()It returns true if gaps between the container and components that border the container are automatically created.
booleangetAutoCreateGaps()It returns true if gaps between components are automatically created.
booleangetHonorsVisibility()It returns whether component visiblity is considered when sizing and positioning components.
floatgetLayoutAlignmentX(Container parent)It returns the alignment along the x axis.
floatgetLayoutAlignmentY(Container parent)It returns the alignment along the y axis.
DimensionmaximumLayoutSize(Container parent)It returns the maximum size for the specified container.

Example 1

FileName: GroupExample.java

Output:

Java Grouplayout 1

Example 2

FileName: GroupExample2.java

Output:

Java Grouplayout 2