| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
JPanel jPanel1 = …;
GroupLayout layout = new GroupLayout(jPanel1);
Based on 32 examples
public class GroupLayout extends Object implements LayoutManager2
{@code GroupLayout} is a {@code LayoutManager} that hierarchically groups components in order to position them in a {@code Container}. {@code GroupLayout} is intended for use by builders, but may be hand-coded as well. Grouping is done by instances of the {@link Group Group} class. {@code 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 {@code Group}, {@code Component}, or gap. A gap can be thought of as an invisible component with a minimum, preferred and maximum size. In addition {@code GroupLayout} supports a preferred gap, whose value comes from {@code 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 {@code LayoutStyle}. The range for {@code Component}s is determined from the {@code Component}'s {@code getMinimumSize}, {@code getPreferredSize} and {@code getMaximumSize} methods. In addition, when adding {@code Component}s you may specify a particular range to use instead of that from the component. The range for a {@code Group} is determined by the type of group. A {@code ParallelGroup}'s range is the maximum of the ranges of its elements. A {@code SequentialGroup}'s range is the sum of the ranges of its elements.
{@code 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 {@code Component} must exist in both a horizontal and vertical group, otherwise an {@code 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.
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.
As {@code c1} is the largest of the three components, the parallel group is sized to {@code c1}. As {@code c2} and {@code c3} are smaller than {@code 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 {@code c2} and {@code c3} were created with an alignment of {@code LEADING}. If the component orientation were right-to-left then {@code c2} and {@code c3} would be positioned on the opposite side.
The following diagram shows a sequential group along both the horizontal and vertical axis.
{@code GroupLayout} provides the ability to insert gaps between {@code Component}s. The size of the gap is determined by an instance of {@code LayoutStyle}. This may be turned on using the {@code setAutoCreateGaps} method. Similarly, you may use the {@code 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.
This layout consists of the following.
Nested Class Summary | |
---|---|
static enum |
Enumeration of the possible ways can align its children. |
abstract class |
provides the basis for the two types of operations supported by : laying out components one after another (javax.swing.GroupLayout.SequentialGroup) or aligned (javax.swing.GroupLayout.ParallelGroup). |
class |
A that aligns and sizes it's children. |
class |
A that positions and sizes its elements sequentially, one after another. |
Field Summary | |
---|---|
static int |
DEFAULT_SIZE
Indicates the size from the component or gap should be used for a particular range value. |
static int |
PREFERRED_SIZE
Indicates the preferred size from the component or gap should be used for a particular range value. |
Constructor Summary | |
---|---|
GroupLayout(Container host) Creates a for the specified . |
Method Summary | |
---|---|
void |
addLayoutComponent(Component component, Object constraints) Notification that a has been added to the parent container. |
void |
addLayoutComponent(String name, Component component) Notification that a has been added to the parent container. |
GroupLayout.ParallelGroup |
createBaselineGroup(boolean resizable, boolean anchorBaselineToTop) Creates and returns a that aligns it's elements along the baseline. |
GroupLayout.ParallelGroup |
Creates and returns a with an alignment of . |
GroupLayout.ParallelGroup |
createParallelGroup(GroupLayout.Alignment alignment) Creates and returns a with the specified alignment. |
GroupLayout.ParallelGroup |
createParallelGroup(GroupLayout.Alignment alignment, boolean resizable) Creates and returns a with the specified alignment and resize behavior. |
GroupLayout.SequentialGroup |
Creates and returns a . |
boolean |
Returns if gaps between the container and components that border the container are automatically created. |
boolean |
Returns if gaps between components are automatically created. |
boolean |
Returns whether component visiblity is considered when sizing and positioning components. |
float |
getLayoutAlignmentX(Container parent) Returns the alignment along the x axis. |
float |
getLayoutAlignmentY(Container parent) Returns the alignment along the y axis. |
LayoutStyle |
Returns the used for calculating the preferred gap between components. |
void |
invalidateLayout(Container parent) Invalidates the layout, indicating that if the layout manager has cached information it should be discarded. |
void |
layoutContainer(Container parent) Lays out the specified container. |
void |
Forces the specified components to have the same size regardless of their preferred, minimum or maximum sizes. |
void |
Forces the specified components to have the same size along the specified axis regardless of their preferred, minimum or maximum sizes. |
Dimension |
maximumLayoutSize(Container parent) Returns the maximum size for the specified container. |
Dimension |
minimumLayoutSize(Container parent) Returns the minimum size for the specified container. |
Dimension |
preferredLayoutSize(Container parent) Returns the preferred size for the specified container. |
void |
removeLayoutComponent(Component component) Notification that a has been removed from the parent container. |
void |
Replaces an existing component with a new one. |
void |
setAutoCreateContainerGaps(boolean autoCreateContainerPadding) Sets whether a gap between the container and components that touch the border of the container should automatically be created. |
void |
setAutoCreateGaps(boolean autoCreatePadding) Sets whether a gap between components should automatically be created. |
void |
setHonorsVisibility(boolean honorsVisibility) Sets whether component visiblity is considered when sizing and positioning components. |
void |
setHonorsVisibility(Component component, Boolean honorsVisibility) Sets whether the component's visiblity is considered for sizing and positioning. |
void |
Sets the that positions and sizes components along the horizontal axis. |
void |
setLayoutStyle(LayoutStyle layoutStyle) Sets the used to calculate the preferred gaps between components. |
void |
Sets the that positions and sizes components along the vertical axis. |
String |
toString() Returns a string representation of this . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int DEFAULT_SIZE
public static final int PREFERRED_SIZE
Constructor Detail |
---|
public GroupLayout(Container host)
host
- the {@code Container} the {@code GroupLayout} is
the {@code LayoutManager} forMethod Detail |
---|
public void addLayoutComponent(Component component, Object constraints)
component
- the component addedconstraints
- description of where to place the componentpublic void addLayoutComponent(String name, Component component)
name
- the string to be associated with the componentcomponent
- the {@code Component} to be addedpublic GroupLayout.ParallelGroup createBaselineGroup(boolean resizable, boolean anchorBaselineToTop)
resizable
- whether the group is resizableanchorBaselineToTop
- whether the baseline is anchored to
the top or bottom of the grouppublic GroupLayout.ParallelGroup createParallelGroup()
public GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment)
alignment
- the alignment for the elements of the grouppublic GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment, boolean resizable)
Baseline alignment is only useful when used along the vertical axis. A {@code ParallelGroup} created with a baseline alignment along the horizontal axis is treated as {@code LEADING}.
Refer to {@link GroupLayout.ParallelGroup ParallelGroup} for details on the behavior of baseline groups.
alignment
- the alignment for the elements of the groupresizable
- {@code 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 grouppublic GroupLayout.SequentialGroup createSequentialGroup()
public boolean getAutoCreateContainerGaps()
public boolean getAutoCreateGaps()
public boolean getHonorsVisibility()
public float getLayoutAlignmentX(Container parent)
parent
- the {@code Container} hosting this {@code LayoutManager}public float getLayoutAlignmentY(Container parent)
parent
- the {@code Container} hosting this {@code LayoutManager}public LayoutStyle getLayoutStyle()
public void invalidateLayout(Container parent)
parent
- the {@code Container} hosting this LayoutManagerpublic void layoutContainer(Container parent)
parent
- the container to be laid outpublic void linkSize(Component[] components)
This can be used multiple times to force any number of components to share the same size.
Linked Components are not be resizable.
components
- the {@code Component}s that are to have the same sizepublic void linkSize(int axis, Component[] components)
This can be used multiple times to force any number of components to share the same size.
Linked {@code Component}s are not be resizable.
axis
- the axis to link the size along; one of
{@code SwingConstants.HORIZONTAL} or
{@code SwingConstans.VERTICAL}components
- the {@code Component}s that are to have the same sizepublic Dimension maximumLayoutSize(Container parent)
parent
- the container to return the size forpublic Dimension minimumLayoutSize(Container parent)
parent
- the container to return the size forpublic Dimension preferredLayoutSize(Container parent)
parent
- the container to return the preferred size forpublic void removeLayoutComponent(Component component)
component
- the component to be removedpublic void replace(Component existingComponent, Component newComponent)
existingComponent
- the component that should be removed
and replaced with {@code newComponent}newComponent
- the component to put in
{@code existingComponent}'s placepublic void setAutoCreateContainerGaps(boolean autoCreateContainerPadding)
autoCreateContainerPadding
- whether a gap between the container and
components that touch the border of the container should
automatically be createdpublic void setAutoCreateGaps(boolean autoCreatePadding)
autoCreatePadding
- whether a gap between components is
automatically createdpublic void setHonorsVisibility(boolean honorsVisibility)
A value of {@code false} is useful when the visibility of components is dynamically adjusted and you don't want surrounding components and the sizing to change.
The specified value is used for components that do not have an explicit visibility specified.
The default is {@code true}.
honorsVisibility
- whether component visiblity is considered when
sizing and positioning componentspublic void setHonorsVisibility(Component component, Boolean honorsVisibility)
If {@code component} is not a child of the {@code Container} this {@code GroupLayout} is managine, it will be added to the {@code Container}.
component
- the componenthonorsVisibility
- whether {@code component}'s visiblity should be
considered for sizing and positioningpublic void setHorizontalGroup(GroupLayout.Group group)
group
- the {@code Group} that positions and sizes
components along the horizontal axispublic void setLayoutStyle(LayoutStyle layoutStyle)
layoutStyle
- the {@code LayoutStyle} to usepublic void setVerticalGroup(GroupLayout.Group group)
group
- the {@code Group} that positions and sizes
components along the vertical axispublic String toString()
toString
in class Object
| |||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |