Interface Layout
- All Known Implementing Classes:
Layout.BorderLayoutInstaller,Layout.ForBoxLayout,Layout.ForFlowLayout,Layout.ForMigLayout,Layout.GridLayoutInstaller,Layout.None,Layout.Unspecific
border(), flow(), grid(int, int)...
and then supplied to the style API through ComponentStyleDelegate.layout(Layout)
so that the layout can then be installed onto a component dynamically.
The various layout types hold necessary information
and implementation logic required for installing the layout onto a component
through the installFor(JComponent) method,
which will be used by the style engine of SwingTree
every time the layout object state changes compared to the previous state
effectively making the layout mechanics of a component fully dynamic.
You may implement this interface to create custom layout configurations
for other kinds of LayoutManager implementations.
This interface also contains various implementations
for supporting the most common types of LayoutManagers.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classTheLayout.BorderLayoutInstallerlayout is a layout that represents aBorderLayoutlayout configuration for a component, which consists of the horizontal gap and vertical gap.static final classTheLayout.ForBoxLayoutlayout is a layout that represents aBoxLayoutlayout configuration for a component, which consists of the axis.static final classAn immutableLayoutimplementation that configures and installs aResponsiveGridFlowLayoutonto a component.static final classAn immutableLayoutimplementation that configures and installs aMigLayoutonto a component.static final classTheLayout.GridLayoutInstallerlayout is a layout that represents aGridLayoutlayout configuration for a component, which consists of the number of rows, number of columns, horizontal gap and vertical gap.static final classTheLayout.Nonelayout removes any existingLayoutManagerfrom a component (sets it tonull), enabling fully manual positioning of child components viaComponent.setBounds(int, int, int, int).static final classTheLayout.Unspecificlayout is a layout that represents the lack of a specific layout being set for a component. -
Method Summary
Modifier and TypeMethodDescriptionstatic Layoutborder()A factory method for creating a layout that installs theBorderLayoutonto a component based on the supplied parameters.static Layoutborder(int horizontalGap, int verticalGap) A factory method for creating a layout that installs theBorderLayoutonto a component based on the supplied parameters.static Layoutbox()static Layoutbooleanstatic Layout.ForFlowLayoutflow()A factory method for creating aLayout.ForFlowLayoutconfiguration that installs aResponsiveGridFlowLayoutwith centered alignment and default gaps of 5 pixels.static Layout.ForFlowLayoutA convenience factory method that creates a centeredLayout.ForFlowLayoutpre-loaded with per-childFlowCellconstraints.static Layout.ForFlowLayoutflow(UI.HorizontalAlignment align) A factory method for creating aLayout.ForFlowLayoutconfiguration that installs aResponsiveGridFlowLayoutwith the given alignment and default gaps of 5 pixels.static Layout.ForFlowLayoutflow(UI.HorizontalAlignment align, int hgap, int vgap) A factory method for creating aLayout.ForFlowLayoutconfiguration that installs aResponsiveGridFlowLayoutonto a component with the given alignment and gaps.static Layout.ForFlowLayoutflow(UI.HorizontalAlignment align, FlowCell... childConstraints) A convenience factory method that creates aLayout.ForFlowLayoutwith the given alignment and per-childFlowCellconstraints pre-loaded.static Layoutgrid(int rows, int cols) A factory method for creating a layout that installs theGridLayoutonto a component based on the supplied parameters.static Layoutgrid(int rows, int cols, int horizontalGap, int verticalGap) A factory method for creating a layout that installs theGridLayoutonto a component based on the supplied parameters.inthashCode()voidinstallFor(JComponent component) Installs this layout for the supplied component.static Layout.ForMigLayoutA convenience overload ofmig(LayoutConstraint)that accepts a plain constraint string instead of aLayoutConstraintobject.static Layout.ForMigLayoutA convenience overload ofmig(LayoutConstraint, LayoutConstraint)that accepts plain constraint strings instead ofLayoutConstraintobjects.static Layout.ForMigLayoutA convenience overload ofmig(LayoutConstraint, LayoutConstraint, LayoutConstraint)that accepts plain constraint strings instead ofLayoutConstraintobjects.static Layout.ForMigLayoutmig(String constr, MigAddConstraint... childConstraints) A convenience factory method that creates aMigLayout-based layout configuration from a plain constraint string together with per-childMigAddConstraints for the component's direct children.static Layout.ForMigLayoutmig(LayoutConstraint constr) A factory method for creating aMigLayout-based layout configuration from a single type-safeLayoutConstraint, with no column or row constraints.static Layout.ForMigLayoutmig(LayoutConstraint constr, LayoutConstraint rowConstr) A factory method for creating aMigLayout-based layout configuration from type-safeLayoutConstraintobjects, without column constraints.static Layout.ForMigLayoutmig(LayoutConstraint constr, LayoutConstraint colConstr, LayoutConstraint rowConstr) The preferred factory method for creating aMigLayout-based layout configuration from type-safeLayoutConstraintobjects.static Layout.ForMigLayoutmig(LayoutConstraint constr, MigAddConstraint... childConstraints) A convenience factory method that creates aMigLayout-based layout configuration from a single type-safeLayoutConstrainttogether with per-childMigAddConstraints for the component's direct children.static Layout.Nonenone()Returns aLayout.Nonelayout that removes any existingLayoutManagerfrom a component (sets it tonull), enabling manual positioning of child components via theirComponent.setBounds(int, int, int, int)method.static Layout.NoneA convenience factory that creates aLayout.Nonelayout pre-loaded with per-childBounds.static LayoutA factory method for creating a layout that does nothing (i.e.
-
Method Details
-
hashCode
int hashCode() -
equals
-
installFor
Installs this layout for the supplied component.- Parameters:
component- The component to install this layout for.
-
unspecific
A factory method for creating a layout that does nothing (i.e. it does not install any layout for a component). This is a no-op layout that can be used to represent the lack of a specific layout being set for a component without having to set the layout tonull.- Returns:
- A layout that does nothing, i.e. it does not install any layout for a component.
-
none
Returns aLayout.Nonelayout that removes any existingLayoutManagerfrom a component (sets it tonull), enabling manual positioning of child components via theirComponent.setBounds(int, int, int, int)method.To also specify the initial bounds of child components declaratively, chain
Layout.None.withChildBounds(Bounds...)on the returned instance, or use thenone(Bounds...)shorthand factory.- Returns:
- A
Layout.Nonelayout that removes any existing layout manager from a component.
-
none
A convenience factory that creates aLayout.Nonelayout pre-loaded with per-childBounds. This is a shorthand for:
WhenLayout.none().withChildBounds(childBounds)installForis called, the layout manager is first removed from the component (enabling absolute positioning), and then each suppliedBoundsentry is applied to the corresponding child by index viaComponent.setBounds(int, int, int, int). Because the underlying storage is a sparseAssociation, you only need to supply bounds for the children you actually want to position; children without an entry are left untouched.- Parameters:
childBounds- TheBoundsto apply to the component's children, in child-index order.- Returns:
- A
Layout.Nonelayout that removes any layout manager and applies the given bounds to the corresponding children.
-
mig
static Layout.ForMigLayout mig(LayoutConstraint constr, LayoutConstraint colConstr, LayoutConstraint rowConstr) The preferred factory method for creating aMigLayout-based layout configuration from type-safeLayoutConstraintobjects.LayoutConstraintis a composable, type-safe wrapper around MigLayout constraint strings. The recommended way to use it is through the constants and factory methods available viaimport static swingtree.UI.*, which can then be combined withLayoutConstraint.and(LayoutConstraint):
Usingimport static swingtree.UI.*; // ... Layout.mig( FILL.and(WRAP(2)), "[shrink][grow]", "[]8[]" )LayoutConstraintinstead of raw strings catches typos at call-site and makes constraint composition explicit and refactor-friendly. See the MigLayout whitepaper for full constraint documentation.The returned
Layout.ForMigLayoutinstance supports fluent chaining to specify per-child component constraints via the variousLayout.ForMigLayout.withChildConstraints(MigAddConstraint...)overloads.- Parameters:
constr- The general layout constraints for theMigLayout(e.g.FILL.and(WRAP(2))).colConstr- The column constraints for theMigLayout(e.g.LayoutConstraint.of("[shrink][grow]")).rowConstr- The row constraints for theMigLayout(e.g.LayoutConstraint.of("[]8[]")).- Returns:
- A
Layout.ForMigLayoutconfigured with the supplied constraints.
-
mig
A factory method for creating aMigLayout-based layout configuration from type-safeLayoutConstraintobjects, without column constraints. This is the preferred approach over the plain-Stringoverloads.See
mig(LayoutConstraint, LayoutConstraint, LayoutConstraint)for full details on theLayoutConstraintAPI and chaining child constraints.- Parameters:
constr- The general layout constraints for theMigLayout.rowConstr- The row constraints for theMigLayout.- Returns:
- A
Layout.ForMigLayoutconfigured with the supplied constraints.
-
mig
A factory method for creating aMigLayout-based layout configuration from a single type-safeLayoutConstraint, with no column or row constraints. This is the preferred approach over the plain-Stringoverload.See
mig(LayoutConstraint, LayoutConstraint, LayoutConstraint)for full details on theLayoutConstraintAPI and chaining child constraints.- Parameters:
constr- The general layout constraints for theMigLayout.- Returns:
- A
Layout.ForMigLayoutconfigured with the supplied constraints.
-
mig
A convenience factory method that creates aMigLayout-based layout configuration from a single type-safeLayoutConstrainttogether with per-childMigAddConstraints for the component's direct children.The
childConstraintsare mapped positionally: the first entry is applied to the first child, the second to the second child, and so on. Excess entries (more constraints than children) are silently ignored; children without a matching entry keep whatever constraint the parentMigLayoutalready has for them.This is a shorthand for:
SeeLayout.mig(constr).withChildConstraints(childConstraints)mig(LayoutConstraint, LayoutConstraint, LayoutConstraint)for full details on theLayoutConstraintAPI.- Parameters:
constr- The general layout constraints for theMigLayout.childConstraints- TheMigAddConstraints to apply to the component's children, in child-index order.- Returns:
- A
Layout.ForMigLayoutconfigured with the supplied constraints.
-
mig
A convenience overload ofmig(LayoutConstraint, LayoutConstraint, LayoutConstraint)that accepts plain constraint strings instead ofLayoutConstraintobjects. Each string is wrapped viaLayoutConstraint.of(String...)before being forwarded.Prefer the
LayoutConstraint-based overloads for new code, as they are composable and less error-prone than raw strings. Click here for more information about MigLayout.- Parameters:
constr- The general layout constraints string for theMigLayout.colConstr- The column constraints string for theMigLayout.rowConstr- The row constraints string for theMigLayout.- Returns:
- A
Layout.ForMigLayoutconfigured with the supplied constraints.
-
mig
A convenience overload ofmig(LayoutConstraint, LayoutConstraint)that accepts plain constraint strings instead ofLayoutConstraintobjects. Each string is wrapped viaLayoutConstraint.of(String...)before being forwarded.Prefer the
LayoutConstraint-based overloads for new code, as they are composable and less error-prone than raw strings. Click here for more information about MigLayout.- Parameters:
constr- The general layout constraints string for theMigLayout.rowConstr- The row constraints string for theMigLayout.- Returns:
- A
Layout.ForMigLayoutconfigured with the supplied constraints.
-
mig
A convenience overload ofmig(LayoutConstraint)that accepts a plain constraint string instead of aLayoutConstraintobject. The string is wrapped viaLayoutConstraint.of(String...)before being forwarded.Prefer the
LayoutConstraint-based overloads for new code, as they are composable and less error-prone than raw strings. In case you are not familiar with the MigLayout constraints, you can find more information about them here.- Parameters:
constr- The general layout constraints string for theMigLayout.- Returns:
- A
Layout.ForMigLayoutconfigured with the supplied constraints.
-
mig
A convenience factory method that creates aMigLayout-based layout configuration from a plain constraint string together with per-childMigAddConstraints for the component's direct children.The
childConstraintsare mapped positionally: the first entry is applied to the first child, the second to the second child, and so on. This is a shorthand for:
Prefer theLayout.mig(constr).withChildConstraints(childConstraints)LayoutConstraint-based overloadmig(LayoutConstraint, MigAddConstraint...)for new code.- Parameters:
constr- The general layout constraints string for theMigLayout.childConstraints- TheMigAddConstraints to apply to the component's children, in child-index order.- Returns:
- A
Layout.ForMigLayoutconfigured with the supplied constraints.
-
flow
A factory method for creating aLayout.ForFlowLayoutconfiguration that installs aResponsiveGridFlowLayoutonto a component with the given alignment and gaps.The returned
Layout.ForFlowLayoutsupports fluent chaining to specify per-childFlowCellconstraints via the variousLayout.ForFlowLayout.withChildConstraints(FlowCell...)overloads, enabling fully reactive responsive layouts when used together withUIForAnySwing.withLayout(sprouts.Val).- Parameters:
align- The alignment for the layout, which has to be one ofhgap- The horizontal gap between components, in pixels.vgap- The vertical gap between component rows, in pixels.- Returns:
- A
Layout.ForFlowLayoutconfigured with the supplied alignment and gaps.
-
flow
A factory method for creating aLayout.ForFlowLayoutconfiguration that installs aResponsiveGridFlowLayoutwith the given alignment and default gaps of 5 pixels.The returned
Layout.ForFlowLayoutsupports fluent chaining to specify per-childFlowCellconstraints. Seeflow(UI.HorizontalAlignment, int, int)for full details.- Parameters:
align- The horizontal alignment for the flow of components.- Returns:
- A
Layout.ForFlowLayoutconfigured with the supplied alignment.
-
flow
A factory method for creating aLayout.ForFlowLayoutconfiguration that installs aResponsiveGridFlowLayoutwith centered alignment and default gaps of 5 pixels.The returned
Layout.ForFlowLayoutsupports fluent chaining to specify per-childFlowCellconstraints. Seeflow(UI.HorizontalAlignment, int, int)for full details.- Returns:
- A
Layout.ForFlowLayoutwith centered alignment and 5-pixel gaps.
-
flow
A convenience factory method that creates a centeredLayout.ForFlowLayoutpre-loaded with per-childFlowCellconstraints. This is a shorthand for:
EachLayout.flow().withChildConstraints(childConstraints)FlowCellis typically created viaUILayoutConstants.AUTO_SPAN(Configurator):
Changing theimport static swingtree.UI.*; // ... Var<Layout> layout = Var.of( Layout.flow( AUTO_SPAN( it -> it.small(12).medium(6) ), AUTO_SPAN( it -> it.small(12).medium(6) ) ) ); UI.panel() .withLayout(layout) .add( label("Left") ) .add( label("Right") );layoutproperty at runtime will reinstall the layout and re-push all childFlowCellconstraints, making the span behaviour fully reactive.- Parameters:
childConstraints- TheFlowCellconstraints to apply to the component's children in child-index order.- Returns:
- A
Layout.ForFlowLayoutwith centered alignment and the given child constraints.
-
flow
A convenience factory method that creates aLayout.ForFlowLayoutwith the given alignment and per-childFlowCellconstraints pre-loaded. This is a shorthand for:
SeeLayout.flow(align).withChildConstraints(childConstraints)flow(FlowCell...)for a usage example and reactive design notes.- Parameters:
align- The horizontal alignment for the flow of components.childConstraints- TheFlowCellconstraints to apply to the component's children in child-index order.- Returns:
- A
Layout.ForFlowLayoutwith the given alignment and child constraints.
-
border
A factory method for creating a layout that installs theBorderLayoutonto a component based on the supplied parameters.- Parameters:
horizontalGap- The horizontal gap for the layout.verticalGap- The vertical gap for the layout.- Returns:
- A layout that installs the
BorderLayoutonto a component.
-
border
A factory method for creating a layout that installs theBorderLayoutonto a component based on the supplied parameters. The installed layout will have a default gap of 0 pixels.- Returns:
- A layout that installs the
BorderLayoutonto a component.
-
grid
A factory method for creating a layout that installs theGridLayoutonto a component based on the supplied parameters.- Parameters:
rows- The number of rows for the layout.cols- The number of columns for the layout.horizontalGap- The horizontal gap for the layout.verticalGap- The vertical gap for the layout.- Returns:
- A layout that installs the
GridLayoutonto a component.
-
grid
A factory method for creating a layout that installs theGridLayoutonto a component based on the supplied parameters. The installed layout will have a default gap of 0 pixels.- Parameters:
rows- The number of rows for the layout.cols- The number of columns for the layout.- Returns:
- A layout that installs the
GridLayoutonto a component.
-
box
A factory method for creating a layout that installs theBoxLayoutonto a component based on the suppliedUI.Axisparameter. The axis determines whether the layout will be a horizontal or verticalBoxLayout.- Parameters:
axis- The axis for the layout, which has to be one of- Returns:
- A layout that installs the
BoxLayoutonto a component.
-
box
A factory method for creating a layout that installs theBoxLayoutonto a component with a default axis ofUI.Axis.X.- Returns:
- A layout that installs the default
BoxLayoutonto a component.
-