Class Layout.ForMigLayout
- All Implemented Interfaces:
Layout
- Enclosing interface:
Layout
Layout implementation that configures and installs a
MigLayout onto a component. It holds three kinds of constraints:
- General layout constraints (
constr) — control global layout behaviour such as wrapping, filling, gaps and hiding mode. - Column constraints (
colConstr) — define the sizing and alignment rules for each column in the grid. - Row constraints (
rowConstr) — define the sizing and alignment rules for each row in the grid. - Per-child component constraints (
childConstraints) — a sortedAssociationmapping child indices (Integer) toMigAddConstraints applied to the component's direct children. Unlike a positional tuple, the association is sparse: you only need to include entries for the children you actually want to configure; children whose index has no entry keep whatever constraint theMigLayoutalready has for them.
Instances are created via the Layout.mig(LayoutConstraint) family of
factory methods and are further configured through the fluent with* wither
methods, which all return a new immutable instance:
import static swingtree.UI.*;
// ...
Layout.mig( FILL.and(WRAP(2)), "[shrink][grow]", "[]8[]" )
.withChildConstraints( RIGHT, GROW_X, RIGHT, GROW_X )
Whenever any property of this configuration changes (detected by the style engine
via equals(java.lang.Object)/hashCode()), the installFor(JComponent) method
is called, which surgically updates only the properties that have changed and
calls JComponent.revalidate() to trigger a layout refresh.-
Nested Class Summary
Nested classes/interfaces inherited from interface swingtree.api.Layout
Layout.BorderLayoutInstaller, Layout.ForBoxLayout, Layout.ForFlowLayout, Layout.ForMigLayout, Layout.GridLayoutInstaller, Layout.None, Layout.Unspecific -
Method Summary
Modifier and TypeMethodDescriptionbooleaninthashCode()voidinstallFor(JComponent component) Installs aMigLayoutonto the supplied component and applies all constraints stored in this configuration.toString()withAddedChildConstraint(String childConstraint) Returns a newLayout.ForMigLayoutinstance with aMigAddConstraintwrapping the supplied string appended to the end of the existing child-constraint tuple.withAddedChildConstraint(MigAddConstraint childConstraint) Returns a newLayout.ForMigLayoutinstance with the suppliedMigAddConstraintappended to the end of the existing child-constraint tuple.withChildConstraint(int index, String childConstraint) Returns a newLayout.ForMigLayoutinstance with theMigAddConstraintat the given child index replaced by a constraint wrapping the supplied string.withChildConstraint(int index, MigAddConstraint childConstraint) Returns a newLayout.ForMigLayoutinstance with theMigAddConstraintat the given child index replaced by the supplied value.withChildConstraints(sprouts.Association<Integer, MigAddConstraint> childConstraints) Returns a newLayout.ForMigLayoutinstance whose per-child component constraints are replaced by the supplied sortedAssociation.withChildConstraints(MigAddConstraint... childConstraints) Returns a newLayout.ForMigLayoutinstance whose per-child component constraints are replaced by the supplied varargs array ofMigAddConstraints.withColumnConstraint(String colConstr) Returns a newLayout.ForMigLayoutinstance with the supplied column constraints and all other properties copied from this instance.withColumnConstraint(LayoutConstraint colConstr) Returns a newLayout.ForMigLayoutinstance with the supplied column constraints and all other properties copied from this instance.withConstraint(String constr) Returns a newLayout.ForMigLayoutinstance with the supplied general layout constraints and all other properties copied from this instance.withConstraint(LayoutConstraint constr) Returns a newLayout.ForMigLayoutinstance with the supplied general layout constraints and all other properties copied from this instance.withRowConstraint(String rowConstr) Returns a newLayout.ForMigLayoutinstance with the supplied row constraints and all other properties copied from this instance.withRowConstraint(LayoutConstraint rowConstr) Returns a newLayout.ForMigLayoutinstance with the supplied row constraints and all other properties copied from this instance.
-
Method Details
-
withConstraint
Returns a newLayout.ForMigLayoutinstance with the supplied general layout constraints and all other properties copied from this instance. This is the preferred overload as it works with the type-safeLayoutConstraintAPI, which supports composition viaLayoutConstraint.and(LayoutConstraint).- Parameters:
constr- The new general layout constraints for theMigLayout.- Returns:
- A new
Layout.ForMigLayoutinstance with the updated layout constraints.
-
withConstraint
Returns a newLayout.ForMigLayoutinstance with the supplied general layout constraints and all other properties copied from this instance. The string is wrapped viaLayoutConstraint.of(String...)and forwarded towithConstraint(LayoutConstraint). PreferwithConstraint(LayoutConstraint)for new code.- Parameters:
constr- The new general layout constraints string for theMigLayout.- Returns:
- A new
Layout.ForMigLayoutinstance with the updated layout constraints.
-
withRowConstraint
Returns a newLayout.ForMigLayoutinstance with the supplied row constraints and all other properties copied from this instance. This is the preferred overload as it works with the type-safeLayoutConstraintAPI, which supports composition viaLayoutConstraint.and(LayoutConstraint).- Parameters:
rowConstr- The new row constraints for theMigLayout.- Returns:
- A new
Layout.ForMigLayoutinstance with the updated row constraints.
-
withRowConstraint
Returns a newLayout.ForMigLayoutinstance with the supplied row constraints and all other properties copied from this instance. The string is wrapped viaLayoutConstraint.of(String...)and forwarded towithRowConstraint(LayoutConstraint). PreferwithRowConstraint(LayoutConstraint)for new code.- Parameters:
rowConstr- The new row constraints string for theMigLayout.- Returns:
- A new
Layout.ForMigLayoutinstance with the updated row constraints.
-
withColumnConstraint
Returns a newLayout.ForMigLayoutinstance with the supplied column constraints and all other properties copied from this instance. This is the preferred overload as it works with the type-safeLayoutConstraintAPI, which supports composition viaLayoutConstraint.and(LayoutConstraint).- Parameters:
colConstr- The new column constraints for theMigLayout.- Returns:
- A new
Layout.ForMigLayoutinstance with the updated column constraints.
-
withColumnConstraint
Returns a newLayout.ForMigLayoutinstance with the supplied column constraints and all other properties copied from this instance. The string is wrapped viaLayoutConstraint.of(String...)and forwarded towithColumnConstraint(LayoutConstraint). PreferwithColumnConstraint(LayoutConstraint)for new code.- Parameters:
colConstr- The new column constraints string for theMigLayout.- Returns:
- A new
Layout.ForMigLayoutinstance with the updated column constraints.
-
withChildConstraints
public Layout.ForMigLayout withChildConstraints(sprouts.Association<Integer, MigAddConstraint> childConstraints) Returns a newLayout.ForMigLayoutinstance whose per-child component constraints are replaced by the supplied sortedAssociation.Keys are child indices (
0= first child,1= second, etc.); the association is sparse, so you only need to include entries for children you actually want to configure. Children whose index has no entry in the association are left untouched. An empty association means no constraints are stored in this layout object; any constraints previously applied to children by an earlierinstallForcall remain in theMigLayoutuntil explicitly overwritten.- Parameters:
childConstraints- A sortedAssociationmapping child indices to theMigAddConstraintto apply.- Returns:
- A new
Layout.ForMigLayoutwith the updated child constraints.
-
withChildConstraints
Returns a newLayout.ForMigLayoutinstance whose per-child component constraints are replaced by the supplied varargs array ofMigAddConstraints.The constraints are mapped positionally to the component's children: the first argument applies to the first child, the second to the second, and so on. Children at indices beyond the supplied array length are left untouched. Passing an empty array stores no constraints in this layout object; any constraints previously applied to children by an earlier
installForcall remain in theMigLayoutuntil explicitly overwritten.This is the most concise way to specify per-child constraints for common cases:
import static swingtree.UI.*; // ... Layout.mig( FILL.and(WRAP(2)) ) .withChildConstraints( RIGHT, GROW_X, RIGHT, GROW_X )- Parameters:
childConstraints- TheMigAddConstraints to apply to the component's children, in child-index order.- Returns:
- A new
Layout.ForMigLayoutwith the updated child constraints.
-
withChildConstraint
Returns a newLayout.ForMigLayoutinstance with theMigAddConstraintat the given child index replaced by the supplied value. All other child constraints and all other properties are copied unchanged.Because the underlying storage is a sparse
Association, no padding is needed: the constraint is stored at exactlyindex, regardless of whether lower indices have entries.- Parameters:
index- The zero-based index of the child whose constraint to update. The first child has index 0.childConstraint- The newMigAddConstraintfor the child atindex.- Returns:
- A new
Layout.ForMigLayoutwith the updated child constraint atindex. - Throws:
IndexOutOfBoundsException- ifindexis negative.
-
withChildConstraint
Returns a newLayout.ForMigLayoutinstance with theMigAddConstraintat the given child index replaced by a constraint wrapping the supplied string. The string is converted viaMigAddConstraint.of(String...)and then forwarded towithChildConstraint(int, MigAddConstraint).Because the underlying storage is a sparse
Association, no padding is needed: the constraint is stored at exactlyindex, regardless of whether lower indices have entries.- Parameters:
index- The zero-based index of the child whose constraint to update.childConstraint- The MigLayout component-constraint string for the child.- Returns:
- A new
Layout.ForMigLayoutwith the updated child constraint atindex. - Throws:
IndexOutOfBoundsException- ifindexis negative.
-
withAddedChildConstraint
Returns a newLayout.ForMigLayoutinstance with the suppliedMigAddConstraintappended to the end of the existing child-constraint tuple. This is a convenient alternative towithChildConstraints(MigAddConstraint...)when building up constraints one at a time:import static swingtree.UI.*; // ... Layout.mig( FILL.and(WRAP(2)) ) .withAddedChildConstraint( RIGHT ) .withAddedChildConstraint( GROW_X ) .withAddedChildConstraint( RIGHT ) .withAddedChildConstraint( GROW_X )- Parameters:
childConstraint- TheMigAddConstraintto append as the next child component constraint.- Returns:
- A new
Layout.ForMigLayoutwith the constraint appended.
-
withAddedChildConstraint
Returns a newLayout.ForMigLayoutinstance with aMigAddConstraintwrapping the supplied string appended to the end of the existing child-constraint tuple. The string is converted viaMigAddConstraint.of(String...)and then forwarded towithAddedChildConstraint(MigAddConstraint).- Parameters:
childConstraint- The MigLayout component-constraint string to append.- Returns:
- A new
Layout.ForMigLayoutwith the constraint appended.
-
hashCode
public int hashCode() -
equals
-
installFor
Installs aMigLayoutonto the supplied component and applies all constraints stored in this configuration.The installation proceeds in three phases:
- Self constraint — if this component's own style holds a
layout constraintand its parent uses aMigLayout, that constraint is pushed into the parent layout so the component is correctly positioned within the parent grid. - Layout manager — if none of the three constraint strings is empty, a
MigLayoutis installed (or updated in-place if one is already present) using the stored general, column, and row constraints. - Child constraints — if the child-constraint tuple is non-empty, each
stored
MigAddConstraintis applied to the corresponding direct child ofcomponent(by position). Only entries that differ from what theMigLayoutalready has are written, andJComponent.revalidate()is called exactly once at the end if anything changed.
- Specified by:
installForin interfaceLayout- Parameters:
component- The component to install theMigLayoutfor.
- Self constraint — if this component's own style holds a
-
toString
-