Package org.jdesktop.layout
Class GroupLayout
- java.lang.Object
-
- org.jdesktop.layout.GroupLayout
-
- All Implemented Interfaces:
java.awt.LayoutManager,java.awt.LayoutManager2
public class GroupLayout extends java.lang.Object implements java.awt.LayoutManager2GroupLayout is a LayoutManager that hierarchically groups components to achieve common, and not so common, layouts. Grouping is done by instances of the Group class. GroupLayout supports two types of groups:
Each Group can contain any number of child groups, Components or gaps. GroupLayout treats each axis independently. That is, there is a group representing the horizontal axis, and a separate group representing the vertical axis. The horizontal group is responsible for setting the x and width of its contents, where as the vertical group is responsible for setting the y and height of its contents.Sequential: A sequential group positions its child elements sequentially, one after another. Parallel: A parallel group positions its child elements in the same space on top of each other. Parallel groups can also align the child elements along their baseline. The following code builds a simple layout 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); layout.setAutocreateGaps(true); layout.setAutocreateContainerGaps(true); GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); hGroup.add(layout.createParallelGroup().add(label1).add(label2)). add(layout.createParallelGroup().add(tf1).add(tf2)); layout.setHorizontalGroup(hGroup); GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup(); vGroup.add(layout.createParallelGroup(GroupLayout.BASELINE).add(label1).add(tf1)). add(layout.createParallelGroup(GroupLayout.BASELINE).add(label2).add(tf2)); layout.setVerticalGroup(vGroup);This layout consists of the following:
- The horizontal axis consists of a sequential group containing two parallel groups. The first parallel group consists of the labels, with the second parallel group consisting of the text fields.
- The vertical axis similarly consists of a sequential group containing two parallel groups. The parallel groups align their contents along the baseline. The first parallel group consists of the first label and text field, and the second group consists of the second label and text field.
- You need not explicitly add the components to the container, this
is indirectly done by using one of the
addmethods. - The various
addmethods ofGroupsreturn themselves. This allows for easy chaining of invocations. For example,group.add(label1).add(label2);is equivalent togroup.add(label1);group.add(label2);. - There are no public constructors for the Groups, instead
use the create methods of
GroupLayout.
setAutocreateGaps()method. Similarly you can use thesetAutocreateContainerGaps()method to insert gaps between the components and the container.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classGroupLayout.GroupGroup provides for commonality between the two types of operations supported byGroupLayout: laying out components one after another (SequentialGroup) or layout on top of each other (ParallelGroup).classGroupLayout.ParallelGroupAGroupthat lays out its elements on top of each other.classGroupLayout.SequentialGroupAGroupthat lays out its elements sequentially, one after another.
-
Field Summary
Fields Modifier and Type Field Description static intBASELINEPossible alignment type.static intCENTERPossible alignment type.static intDEFAULT_SIZEPossible value for the add methods that takes a Component.static intHORIZONTALPossible argument when linking sizes of components.static intLEADINGPossible alignment type.static intPREFERRED_SIZEPossible value for the add methods that takes a Component.static intTRAILINGPossible alignment type.static intVERTICALPossible argument when linking sizes of components.
-
Constructor Summary
Constructors Constructor Description GroupLayout(java.awt.Container host)Creates a GroupLayout for the specified JComponent.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddLayoutComponent(java.awt.Component component, java.lang.Object constraints)Notification that aComponenthas been added to the parent container.voidaddLayoutComponent(java.lang.String name, java.awt.Component component)Notification that aComponenthas been added to the parent container.GroupLayout.ParallelGroupcreateBaselineGroup(boolean resizable, boolean anchorBaselineToTop)Creates and returns aParallelGroupthat aligns it's elements along the baseline.GroupLayout.ParallelGroupcreateParallelGroup()Creates and returns aParallelGroupwith aLEADINGalignment.GroupLayout.ParallelGroupcreateParallelGroup(int alignment)Creates and returns anParallelGroup.GroupLayout.ParallelGroupcreateParallelGroup(int alignment, boolean resizable)Creates and returns anParallelGroup.GroupLayout.SequentialGroupcreateSequentialGroup()Creates and returns aSequentialGroup.booleangetAutocreateContainerGaps()Returns whether or not gaps between the container and the first/last components should automatically be created.booleangetAutocreateGaps()Returns true if gaps between components are automatically be created.booleangetHonorsVisibility()Returns whether component visiblity is considered when sizing and positioning components.GroupLayout.GroupgetHorizontalGroup()Returns theGroupthat is responsible for layout along the horizontal axis.floatgetLayoutAlignmentX(java.awt.Container parent)Returns the alignment along the x axis.floatgetLayoutAlignmentY(java.awt.Container parent)Returns the alignment along the y axis.LayoutStylegetLayoutStyle()Returns the LayoutStyle instance to useGroupLayout.GroupgetVerticalGroup()Returns theParallelGroupthat is responsible for layout along the vertical axis.voidinvalidateLayout(java.awt.Container parent)Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.voidlayoutContainer(java.awt.Container parent)Lays out the specified container.voidlinkSize(java.awt.Component[] components)Forces the set of components to have the same size.voidlinkSize(java.awt.Component[] components, int axis)Forces the set of components to have the same size.java.awt.DimensionmaximumLayoutSize(java.awt.Container parent)Returns the maximum size for the specified container.java.awt.DimensionminimumLayoutSize(java.awt.Container parent)Returns the minimum size for the specified container.java.awt.DimensionpreferredLayoutSize(java.awt.Container parent)Returns the preferred size for the specified container.voidremoveLayoutComponent(java.awt.Component component)Notification that aComponenthas been removed from the parent container.voidreplace(java.awt.Component existingComponent, java.awt.Component newComponent)Removes an existing component replacing it with the specified component.voidsetAutocreateContainerGaps(boolean autocreatePadding)Sets whether or not gaps between the container and the first/last components should automatically be created.voidsetAutocreateGaps(boolean autocreatePadding)Sets whether or not a gap between components should automatically be created.voidsetHonorsVisibility(boolean honorsVisibility)Sets whether component visiblity is considered when sizing and positioning components.voidsetHonorsVisibility(java.awt.Component component, java.lang.Boolean honorsVisibility)Sets whether the component's visiblity is considered for sizing and positioning.voidsetHorizontalGroup(GroupLayout.Group group)Sets theGroupthat is responsible for layout along the horizontal axis.voidsetLayoutStyle(LayoutStyle layoutStyle)Sets the LayoutStyle this GroupLayout is to use.voidsetVerticalGroup(GroupLayout.Group group)Sets theGroupthat is responsible for layout along the vertical axis.java.lang.StringtoString()Returns a textual description of this GroupLayout.
-
-
-
Field Detail
-
HORIZONTAL
public static final int HORIZONTAL
Possible argument when linking sizes of components. Specifies the the two component should share the same size along the horizontal axis.
-
VERTICAL
public static final int VERTICAL
Possible argument when linking sizes of components. Specifies the the two component should share the same size along the vertical axis.
-
LEADING
public static final int LEADING
Possible alignment type. Indicates the elements should be aligned to the origin. For the horizontal axis with a left to right orientation this means aligned to the left.- See Also:
createParallelGroup(int), Constant Field Values
-
TRAILING
public static final int TRAILING
Possible alignment type. Indicates the elements should be aligned to the end. For the horizontal axis with a left to right orientation this means aligned to the right.- See Also:
createParallelGroup(int), Constant Field Values
-
CENTER
public static final int CENTER
Possible alignment type. Indicates the elements should centered in the spaced provided.- See Also:
createParallelGroup(int), Constant Field Values
-
BASELINE
public static final int BASELINE
Possible alignment type. Indicates the elements should aligned along their baseline.- See Also:
createParallelGroup(int), Constant Field Values
-
DEFAULT_SIZE
public static final int DEFAULT_SIZE
Possible value for the add methods that takes a Component. Indicates the size from the component should be used.- See Also:
- Constant Field Values
-
PREFERRED_SIZE
public static final int PREFERRED_SIZE
Possible value for the add methods that takes a Component. Indicates the preferred size should be used.- See Also:
- Constant Field Values
-
-
Method Detail
-
setHonorsVisibility
public void setHonorsVisibility(boolean honorsVisibility)
Sets whether component visiblity is considered when sizing and positioning components. A value oftrueindicates that non-visible components should not be treated as part of the layout. A value offalseindicates that components should be positioned and sized regardless of visibility.A value of
falseis 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
true.- Parameters:
honorsVisibility- whether component visiblity is considered when sizing and positioning components- See Also:
setHonorsVisibility(Component,Boolean)
-
getHonorsVisibility
public boolean getHonorsVisibility()
Returns whether component visiblity is considered when sizing and positioning components.- Returns:
- whether component visiblity is considered when sizing and positioning components
-
setHonorsVisibility
public void setHonorsVisibility(java.awt.Component component, java.lang.Boolean honorsVisibility)Sets whether the component's visiblity is considered for sizing and positioning. A value ofBoolean.TRUEindicates that ifcomponentis not visible it should not be treated as part of the layout. A value offalseindicates thatcomponentis positioned and sized regardless of it's visibility. A value ofnullindicates the value specified by the single argument methodsetHonorsVisibilityshould be used.If
componentis not a child of theContainerthisGroupLayoutis managing, it will be added to theContainer.- Parameters:
component- the componenthonorsVisibility- whethercomponent's visiblity should be considered for sizing and positioning- Throws:
java.lang.IllegalArgumentException- ifcomponentisnull- See Also:
setHonorsVisibility(boolean)
-
toString
public java.lang.String toString()
Returns a textual description of this GroupLayout. The return value is intended for debugging purposes only.- Overrides:
toStringin classjava.lang.Object- Returns:
- textual description of this GroupLayout
-
setAutocreateGaps
public void setAutocreateGaps(boolean autocreatePadding)
Sets whether or not a gap between components should automatically be created. For example, if this is true and you add two components to aSequentialGroupa gap between the two will automatically be created. The default is false.- Parameters:
autocreatePadding- whether or not to automatically created a gap between components and the container
-
getAutocreateGaps
public boolean getAutocreateGaps()
Returns true if gaps between components are automatically be created.- Returns:
- true if gaps between components should automatically be created
-
setAutocreateContainerGaps
public void setAutocreateContainerGaps(boolean autocreatePadding)
Sets whether or not gaps between the container and the first/last components should automatically be created. The default is false.- Parameters:
autocreatePadding- whether or not to automatically create gaps between the container and first/last components.
-
getAutocreateContainerGaps
public boolean getAutocreateContainerGaps()
Returns whether or not gaps between the container and the first/last components should automatically be created. The default is false.- Returns:
- whether or not the gaps between the container and the first/last components should automatically be created
-
setHorizontalGroup
public void setHorizontalGroup(GroupLayout.Group group)
Sets theGroupthat is responsible for layout along the horizontal axis.- Parameters:
group-Groupresponsible for layout along the horizontal axis- Throws:
java.lang.IllegalArgumentException- if group is null
-
getHorizontalGroup
public GroupLayout.Group getHorizontalGroup()
Returns theGroupthat is responsible for layout along the horizontal axis.- Returns:
ParallelGroupresponsible for layout along the horizontal axis.
-
setVerticalGroup
public void setVerticalGroup(GroupLayout.Group group)
Sets theGroupthat is responsible for layout along the vertical axis.- Parameters:
group-Groupresponsible for layout along the vertical axis.- Throws:
java.lang.IllegalArgumentException- if group is null.
-
getVerticalGroup
public GroupLayout.Group getVerticalGroup()
Returns theParallelGroupthat is responsible for layout along the vertical axis.- Returns:
ParallelGroupresponsible for layout along the vertical axis.
-
createSequentialGroup
public GroupLayout.SequentialGroup createSequentialGroup()
Creates and returns aSequentialGroup.- Returns:
- a new
SequentialGroup
-
createParallelGroup
public GroupLayout.ParallelGroup createParallelGroup()
Creates and returns aParallelGroupwith aLEADINGalignment. This is a cover method for the more generalcreateParallelGroup(int)method.- Returns:
- a new ParallelGroup
- See Also:
createParallelGroup(int)
-
createParallelGroup
public GroupLayout.ParallelGroup createParallelGroup(int alignment)
Creates and returns anParallelGroup. The alignment specifies how children elements should be positioned when the the parallel group is given more space than necessary. For example, if a ParallelGroup with an alignment of TRAILING is given 100 pixels and a child only needs 50 pixels, the child will be positioned at the position 50.- Parameters:
alignment- alignment for the elements of the Group, one ofLEADING,TRAILING,CENTERorBASELINE.- Returns:
- a new
ParallelGroup - Throws:
java.lang.IllegalArgumentException- if alignment is not one ofLEADING,TRAILING,CENTERorBASELINE
-
createParallelGroup
public GroupLayout.ParallelGroup createParallelGroup(int alignment, boolean resizable)
Creates and returns anParallelGroup. The alignment specifies how children elements should be positioned when the the parallel group is given more space than necessary. For example, if a ParallelGroup with an alignment of TRAILING is given 100 pixels and a child only needs 50 pixels, the child will be positioned at the position 50.- Parameters:
alignment- alignment for the elements of the Group, one ofLEADING,TRAILING,CENTERorBASELINE.resizable- whether or not the group is resizable. If the group is not resizable the min/max size will be the same as the preferred.- Returns:
- a new
ParallelGroup - Throws:
java.lang.IllegalArgumentException- if alignment is not one ofLEADING,TRAILING,CENTERorBASELINE
-
createBaselineGroup
public GroupLayout.ParallelGroup createBaselineGroup(boolean resizable, boolean anchorBaselineToTop)
Creates and returns aParallelGroupthat aligns it's elements along the baseline.- Parameters:
resizable- whether the group is resizableanchorBaselineToTop- whether the baseline is anchored to the top or bottom of the group- See Also:
createBaselineGroup(boolean, boolean),GroupLayout.ParallelGroup
-
linkSize
public void linkSize(java.awt.Component[] components)
Forces the set of components to have the same size. This can be used multiple times to force any number of components to share the same size.Linked Components are not be resizable.
- Parameters:
components- Components to force to have same size.- Throws:
java.lang.IllegalArgumentException- ifcomponentsis null, or contains null.
-
linkSize
public void linkSize(java.awt.Component[] components, int axis)Forces the set of components to have the same size. This can be used multiple times to force any number of components to share the same size.Linked Components are not be resizable.
- Parameters:
components- Components to force to have same size.axis- Axis to bind size, one of HORIZONTAL, VERTICAL or HORIZONTAL | VERTICAL- Throws:
java.lang.IllegalArgumentException- ifcomponentsis null, or contains null.java.lang.IllegalArgumentException- ifaxisdoes not containHORIZONTALorVERTICAL
-
replace
public void replace(java.awt.Component existingComponent, java.awt.Component newComponent)Removes an existing component replacing it with the specified component.- Parameters:
existingComponent- the Component that should be removed and replaced with newComponentnewComponent- the Component to put in existingComponents place- Throws:
java.lang.IllegalArgumentException- is either of the Components are null or if existingComponent is not being managed by this layout manager
-
setLayoutStyle
public void setLayoutStyle(LayoutStyle layoutStyle)
Sets the LayoutStyle this GroupLayout is to use. A value of null can be used to indicate the shared instance of LayoutStyle should be used.- Parameters:
layoutStyle- the LayoutStyle to use
-
getLayoutStyle
public LayoutStyle getLayoutStyle()
Returns the LayoutStyle instance to use- Returns:
- the LayoutStyle instance to use
-
addLayoutComponent
public void addLayoutComponent(java.lang.String name, java.awt.Component component)Notification that aComponenthas been added to the parent container. Developers should not invoke this method directly, instead you should use one of theGroupmethods to add aComponent.- Specified by:
addLayoutComponentin interfacejava.awt.LayoutManager- Parameters:
name- the string to be associated with the componentcomponent- theComponentto be added
-
removeLayoutComponent
public void removeLayoutComponent(java.awt.Component component)
Notification that aComponenthas been removed from the parent container. You should not invoke this method directly, instead invokeremoveon the parentContainer.- Specified by:
removeLayoutComponentin interfacejava.awt.LayoutManager- Parameters:
component- the component to be removed- See Also:
Component.remove(java.awt.MenuComponent)
-
preferredLayoutSize
public java.awt.Dimension preferredLayoutSize(java.awt.Container parent)
Returns the preferred size for the specified container.- Specified by:
preferredLayoutSizein interfacejava.awt.LayoutManager- Parameters:
parent- the container to return size for- Throws:
java.lang.IllegalArgumentException- ifparentis not the sameContainerthat this was created withjava.lang.IllegalStateException- if any of the components added to this layout are not in both a horizontal and vertical group- See Also:
Container.getPreferredSize()
-
minimumLayoutSize
public java.awt.Dimension minimumLayoutSize(java.awt.Container parent)
Returns the minimum size for the specified container.- Specified by:
minimumLayoutSizein interfacejava.awt.LayoutManager- Parameters:
parent- the container to return size for- Throws:
java.lang.IllegalArgumentException- ifparentis not the sameContainerthat this was created withjava.lang.IllegalStateException- if any of the components added to this layout are not in both a horizontal and vertical group- See Also:
Container.getMinimumSize()
-
layoutContainer
public void layoutContainer(java.awt.Container parent)
Lays out the specified container.- Specified by:
layoutContainerin interfacejava.awt.LayoutManager- Parameters:
parent- the container to be laid out- Throws:
java.lang.IllegalStateException- if any of the components added to this layout are not in both a horizontal and vertical group
-
addLayoutComponent
public void addLayoutComponent(java.awt.Component component, java.lang.Object constraints)Notification that aComponenthas been added to the parent container. You should not invoke this method directly, instead you should use one of theGroupmethods to add aComponent.- Specified by:
addLayoutComponentin interfacejava.awt.LayoutManager2- Parameters:
component- The component addedconstraints- Description of where to place the component.
-
maximumLayoutSize
public java.awt.Dimension maximumLayoutSize(java.awt.Container parent)
Returns the maximum size for the specified container.- Specified by:
maximumLayoutSizein interfacejava.awt.LayoutManager2- Parameters:
parent- the container to return size for- Throws:
java.lang.IllegalArgumentException- ifparentis not the sameContainerthat this was created withjava.lang.IllegalStateException- if any of the components added to this layout are not in both a horizontal and vertical group- See Also:
Container.getMaximumSize()
-
getLayoutAlignmentX
public float getLayoutAlignmentX(java.awt.Container parent)
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.- Specified by:
getLayoutAlignmentXin interfacejava.awt.LayoutManager2- Parameters:
parent- Container hosting this LayoutManager- Returns:
- alignment
- Throws:
java.lang.IllegalArgumentException- ifparentis not the sameContainerthat this was created with
-
getLayoutAlignmentY
public float getLayoutAlignmentY(java.awt.Container parent)
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.- Specified by:
getLayoutAlignmentYin interfacejava.awt.LayoutManager2- Parameters:
parent- Container hosting this LayoutManager- Returns:
- alignment
- Throws:
java.lang.IllegalArgumentException- ifparentis not the sameContainerthat this was created with
-
invalidateLayout
public void invalidateLayout(java.awt.Container parent)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.- Specified by:
invalidateLayoutin interfacejava.awt.LayoutManager2- Parameters:
parent- Container hosting this LayoutManager- Throws:
java.lang.IllegalArgumentException- ifparentis not the sameContainerthat this was created with
-
-