LayoutManager
, LayoutManager2
public class SpringLayout extends Object implements LayoutManager2
SpringLayout
lays out the children of its associated container according to a set of constraints. See How to Use SpringLayout in The Java Tutorial for examples of using SpringLayout
. Each constraint, represented by a Spring
object, controls the vertical or horizontal distance between two component edges. The edges can belong to any child of the container, or to the container itself. For example, the allowable width of a component can be expressed using a constraint that controls the distance between the west (left) and east (right) edges of the component. The allowable y coordinates for a component can be expressed by constraining the distance between the north (top) edge of the component and the north edge of its container.
Every child of a SpringLayout
-controlled container, as well as the container itself, has exactly one set of constraints associated with it. These constraints are represented by a SpringLayout.Constraints
object. By default, SpringLayout
creates constraints that make their associated component have the minimum, preferred, and maximum sizes returned by the component's Component.getMinimumSize()
, Component.getPreferredSize()
, and Component.getMaximumSize()
methods. The x and y positions are initially not constrained, so that until you constrain them the Component
will be positioned at 0,0 relative to the Insets
of the parent Container
.
You can change a component's constraints in several ways. You can use one of the putConstraint
methods to establish a spring linking the edges of two components within the same container. Or you can get the appropriate SpringLayout.Constraints
object using getConstraints
and then modify one or more of its springs. Or you can get the spring for a particular edge of a component using getConstraint
, and modify it. You can also associate your own SpringLayout.Constraints
object with a component by specifying the constraints object when you add the component to its container (using Container.add(Component, Object)
).
The Spring
object representing each constraint has a minimum, preferred, maximum, and current value. The current value of the spring is somewhere between the minimum and maximum values, according to the formula given in the Spring.sum(javax.swing.Spring, javax.swing.Spring)
method description. When the minimum, preferred, and maximum values are the same, the current value is always equal to them; this inflexible spring is called a strut. You can create struts using the factory method Spring.constant(int)
. The Spring
class also provides factory methods for creating other kinds of springs, including springs that depend on other springs.
In a SpringLayout
, the position of each edge is dependent on the position of just one other edge. If a constraint is subsequently added to create a new binding for an edge, the previous binding is discarded and the edge remains dependent on a single edge. Springs should only be attached between edges of the container and its immediate children; the behavior of the SpringLayout
when presented with constraints linking the edges of components from different containers (either internal or external) is undefined.
Note: Unlike many layout managers,SpringLayout
doesn't automatically set the location of the components it manages. If you hand-code a GUI that usesSpringLayout
, remember to initialize component locations by constraining the west/east and north/south locations.Depending on the constraints you use, you may also need to set the size of the container explicitly.
Despite the simplicity of SpringLayout
, it can emulate the behavior of most other layout managers. For some features, such as the line breaking provided by FlowLayout
, you'll need to create a special-purpose subclass of the Spring
class.
SpringLayout
also provides a way to solve many of the difficult layout problems that cannot be solved by nesting combinations of Box
es. That said, SpringLayout
honors the LayoutManager2
contract correctly and so can be nested with other layout managers -- a technique that can be preferable to creating the constraints implied by the other layout managers.
The asymptotic complexity of the layout operation of a SpringLayout
is linear in the number of constraints (and/or components).
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans
package. Please see XMLEncoder
.
Modifier and Type | Class | Description |
---|---|---|
static class |
SpringLayout.Constraints |
A Constraints object holds the constraints that govern the way a component's size and position change in a container controlled by a SpringLayout . |
Modifier and Type | Field | Description |
---|---|---|
static final String |
BASELINE |
Specifies the baseline of a component. |
static final String |
EAST |
Specifies the right edge of a component's bounding rectangle. |
static final String |
HEIGHT |
Specifies the height of a component's bounding rectangle. |
static final String |
HORIZONTAL_CENTER |
Specifies the horizontal center of a component's bounding rectangle. |
static final String |
NORTH |
Specifies the top edge of a component's bounding rectangle. |
static final String |
SOUTH |
Specifies the bottom edge of a component's bounding rectangle. |
static final String |
VERTICAL_CENTER |
Specifies the vertical center of a component's bounding rectangle. |
static final String |
WEST |
Specifies the left edge of a component's bounding rectangle. |
static final String |
WIDTH |
Specifies the width of a component's bounding rectangle. |
Constructor | Description |
---|---|
SpringLayout() |
Constructs a new SpringLayout . |
Modifier and Type | Method | Description |
---|---|---|
void |
addLayoutComponent |
If constraints is an instance of SpringLayout.Constraints , associates the constraints with the specified component. |
void |
addLayoutComponent |
Has no effect, since this layout manager does not use a per-component string. |
Spring |
getConstraint |
Returns the spring controlling the distance between the specified edge of the component and the top or left edge of its parent. |
SpringLayout.Constraints |
getConstraints |
Returns the constraints for the specified component. |
float |
getLayoutAlignmentX |
Returns 0.5f (centered). |
float |
getLayoutAlignmentY |
Returns 0.5f (centered). |
void |
invalidateLayout |
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded. |
void |
layoutContainer |
Lays out the specified container. |
Dimension |
maximumLayoutSize |
Calculates the maximum size dimensions for the specified container, given the components it contains. |
Dimension |
minimumLayoutSize |
Calculates the minimum size dimensions for the specified container, given the components it contains. |
Dimension |
preferredLayoutSize |
Calculates the preferred size dimensions for the specified container, given the components it contains. |
void |
putConstraint |
Links edge e1 of component c1 to edge e2 of component c2 , with a fixed distance between the edges. |
void |
putConstraint |
Links edge e1 of component c1 to edge e2 of component c2 . |
void |
removeLayoutComponent |
Removes the constraints associated with the specified component. |
public static final String NORTH
public static final String SOUTH
public static final String EAST
public static final String WEST
public static final String HORIZONTAL_CENTER
public static final String VERTICAL_CENTER
public static final String BASELINE
public static final String WIDTH
public static final String HEIGHT
public SpringLayout()
SpringLayout
.public void addLayoutComponent(String name, Component c)
addLayoutComponent
in interface LayoutManager
name
- the string to be associated with the componentc
- the component to be addedpublic void removeLayoutComponent(Component c)
removeLayoutComponent
in interface LayoutManager
c
- the component being removed from the containerpublic Dimension minimumLayoutSize(Container parent)
LayoutManager
minimumLayoutSize
in interface LayoutManager
parent
- the component to be laid outpublic Dimension preferredLayoutSize(Container parent)
LayoutManager
preferredLayoutSize
in interface LayoutManager
parent
- the container to be laid outpublic Dimension maximumLayoutSize(Container parent)
LayoutManager2
maximumLayoutSize
in interface LayoutManager2
parent
- the target containerpublic void addLayoutComponent(Component component, Object constraints)
constraints
is an instance of SpringLayout.Constraints
, associates the constraints with the specified component.addLayoutComponent
in interface LayoutManager2
component
- the component being addedconstraints
- the component's constraintspublic float getLayoutAlignmentX(Container p)
getLayoutAlignmentX
in interface LayoutManager2
p
- the target containerpublic float getLayoutAlignmentY(Container p)
getLayoutAlignmentY
in interface LayoutManager2
p
- the target containerpublic void invalidateLayout(Container p)
LayoutManager2
invalidateLayout
in interface LayoutManager2
p
- the target containerpublic void putConstraint(String e1, Component c1, int pad, String e2, Component c2)
e1
of component c1
to edge e2
of component c2
, with a fixed distance between the edges. This constraint will cause the assignment value(e1, c1) = value(e2, c2) + padto take place during all subsequent layout operations.
e1
- the edge of the dependentc1
- the component of the dependentpad
- the fixed distance between dependent and anchore2
- the edge of the anchorc2
- the component of the anchorpublic void putConstraint(String e1, Component c1, Spring s, String e2, Component c2)
e1
of component c1
to edge e2
of component c2
. As edge (e2, c2)
changes value, edge (e1, c1)
will be calculated by taking the (spring) sum of (e2, c2)
and s
. Each edge must have one of the following values: SpringLayout.NORTH
, SpringLayout.SOUTH
, SpringLayout.EAST
, SpringLayout.WEST
, SpringLayout.VERTICAL_CENTER
, SpringLayout.HORIZONTAL_CENTER
or SpringLayout.BASELINE
.e1
- the edge of the dependentc1
- the component of the dependents
- the spring linking dependent and anchore2
- the edge of the anchorc2
- the component of the anchorpublic SpringLayout.Constraints getConstraints(Component c)
GridBagLayout
getConstraints
method, this method does not clone constraints. If no constraints have been associated with this component, this method returns a default constraints object positioned at 0,0 relative to the parent's Insets and its width/height constrained to the minimum, maximum, and preferred sizes of the component. The size characteristics are not frozen at the time this method is called; instead this method returns a constraints object whose characteristics track the characteristics of the component as they change.c
- the component whose constraints will be returnedpublic Spring getConstraint(String edgeName, Component c)
SpringLayout
so that the layout operation always terminates.edgeName
- must be one of SpringLayout.NORTH
, SpringLayout.SOUTH
, SpringLayout.EAST
, SpringLayout.WEST
, SpringLayout.VERTICAL_CENTER
, SpringLayout.HORIZONTAL_CENTER
or SpringLayout.BASELINE
c
- the component whose edge spring is desiredpublic void layoutContainer(Container parent)
LayoutManager
layoutContainer
in interface LayoutManager
parent
- the container to be laid out
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/javax/swing/SpringLayout.html