- java.lang.Object
-
- javax.swing.SpringLayout
-
- All Implemented Interfaces:
- LayoutManager, LayoutManager2
public class SpringLayout extends Object implements LayoutManager2
ASpringLayout
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 usingSpringLayout
.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 aSpringLayout.Constraints
object. By default,SpringLayout
creates constraints that make their associated component have the minimum, preferred, and maximum sizes returned by the component'sComponent.getMinimumSize()
,Component.getPreferredSize()
, andComponent.getMaximumSize()
methods. The x and y positions are initially not constrained, so that until you constrain them theComponent
will be positioned at 0,0 relative to theInsets
of the parentContainer
.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 appropriateSpringLayout.Constraints
object usinggetConstraints
and then modify one or more of its springs. Or you can get the spring for a particular edge of a component usinggetConstraint
, and modify it. You can also associate your ownSpringLayout.Constraints
object with a component by specifying the constraints object when you add the component to its container (usingContainer.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 theSpring.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 methodSpring.constant(int)
. TheSpring
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 theSpringLayout
when presented with constraints linking the edges of components from different containers (either internal or external) is undefined.SpringLayout vs. Other Layout Managers
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 byFlowLayout
, you'll need to create a special-purpose subclass of theSpring
class.SpringLayout
also provides a way to solve many of the difficult layout problems that cannot be solved by nesting combinations ofBox
es. That said,SpringLayout
honors theLayoutManager2
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 JavaBeansTM has been added to the
java.beans
package. Please seeXMLEncoder
.- Since:
- 1.4
- See Also:
Spring
,SpringLayout.Constraints
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static class
SpringLayout.Constraints
AConstraints
object holds the constraints that govern the way a component's size and position change in a container controlled by aSpringLayout
.
-
Field Summary
Fields Modifier and Type Field and Description static String
BASELINE
Specifies the baseline of a component.static String
EAST
Specifies the right edge of a component's bounding rectangle.static String
HEIGHT
Specifies the height of a component's bounding rectangle.static String
HORIZONTAL_CENTER
Specifies the horizontal center of a component's bounding rectangle.static String
NORTH
Specifies the top edge of a component's bounding rectangle.static String
SOUTH
Specifies the bottom edge of a component's bounding rectangle.static String
VERTICAL_CENTER
Specifies the vertical center of a component's bounding rectangle.static String
WEST
Specifies the left edge of a component's bounding rectangle.static String
WIDTH
Specifies the width of a component's bounding rectangle.
-
Constructor Summary
Constructors Constructor and Description SpringLayout()
Constructs a newSpringLayout
.
-
Method Summary
Methods Modifier and Type Method and Description void
addLayoutComponent(Component component, Object constraints)
Ifconstraints
is an instance ofSpringLayout.Constraints
, associates the constraints with the specified component.void
addLayoutComponent(String name, Component c)
Has no effect, since this layout manager does not use a per-component string.Spring
getConstraint(String edgeName, Component c)
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(Component c)
Returns the constraints for the specified component.float
getLayoutAlignmentX(Container p)
Returns 0.5f (centered).float
getLayoutAlignmentY(Container p)
Returns 0.5f (centered).void
invalidateLayout(Container p)
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.Dimension
maximumLayoutSize(Container parent)
Calculates the maximum size dimensions for the specified container, given the components it contains.Dimension
minimumLayoutSize(Container parent)
Calculates the minimum size dimensions for the specified container, given the components it contains.Dimension
preferredLayoutSize(Container parent)
Calculates the preferred size dimensions for the specified container, given the components it contains.void
putConstraint(String e1, Component c1, int pad, String e2, Component c2)
Links edgee1
of componentc1
to edgee2
of componentc2
, with a fixed distance between the edges.void
putConstraint(String e1, Component c1, Spring s, String e2, Component c2)
Links edgee1
of componentc1
to edgee2
of componentc2
.void
removeLayoutComponent(Component c)
Removes the constraints associated with the specified component.
-
-
-
Field Detail
-
NORTH
public static final String NORTH
Specifies the top edge of a component's bounding rectangle.- See Also:
- Constant Field Values
-
SOUTH
public static final String SOUTH
Specifies the bottom edge of a component's bounding rectangle.- See Also:
- Constant Field Values
-
EAST
public static final String EAST
Specifies the right edge of a component's bounding rectangle.- See Also:
- Constant Field Values
-
WEST
public static final String WEST
Specifies the left edge of a component's bounding rectangle.- See Also:
- Constant Field Values
-
HORIZONTAL_CENTER
public static final String HORIZONTAL_CENTER
Specifies the horizontal center of a component's bounding rectangle.- Since:
- 1.6
- See Also:
- Constant Field Values
-
VERTICAL_CENTER
public static final String VERTICAL_CENTER
Specifies the vertical center of a component's bounding rectangle.- Since:
- 1.6
- See Also:
- Constant Field Values
-
BASELINE
public static final String BASELINE
Specifies the baseline of a component.- Since:
- 1.6
- See Also:
- Constant Field Values
-
WIDTH
public static final String WIDTH
Specifies the width of a component's bounding rectangle.- Since:
- 1.6
- See Also:
- Constant Field Values
-
HEIGHT
public static final String HEIGHT
Specifies the height of a component's bounding rectangle.- Since:
- 1.6
- See Also:
- Constant Field Values
-
-
Method Detail
-
addLayoutComponent
public void addLayoutComponent(String name, Component c)
Has no effect, since this layout manager does not use a per-component string.- Specified by:
addLayoutComponent
in interfaceLayoutManager
- Parameters:
name
- the string to be associated with the componentc
- the component to be added
-
removeLayoutComponent
public void removeLayoutComponent(Component c)
Removes the constraints associated with the specified component.- Specified by:
removeLayoutComponent
in interfaceLayoutManager
- Parameters:
c
- the component being removed from the container
-
minimumLayoutSize
public Dimension minimumLayoutSize(Container parent)
Description copied from interface:LayoutManager
Calculates the minimum size dimensions for the specified container, given the components it contains.- Specified by:
minimumLayoutSize
in interfaceLayoutManager
- Parameters:
parent
- the component to be laid out- See Also:
LayoutManager.preferredLayoutSize(java.awt.Container)
-
preferredLayoutSize
public Dimension preferredLayoutSize(Container parent)
Description copied from interface:LayoutManager
Calculates the preferred size dimensions for the specified container, given the components it contains.- Specified by:
preferredLayoutSize
in interfaceLayoutManager
- Parameters:
parent
- the container to be laid out- See Also:
LayoutManager.minimumLayoutSize(java.awt.Container)
-
maximumLayoutSize
public Dimension maximumLayoutSize(Container parent)
Description copied from interface:LayoutManager2
Calculates the maximum size dimensions for the specified container, given the components it contains.- Specified by:
maximumLayoutSize
in interfaceLayoutManager2
- See Also:
Component.getMaximumSize()
,LayoutManager
-
addLayoutComponent
public void addLayoutComponent(Component component, Object constraints)
Ifconstraints
is an instance ofSpringLayout.Constraints
, associates the constraints with the specified component.- Specified by:
addLayoutComponent
in interfaceLayoutManager2
- Parameters:
component
- the component being addedconstraints
- the component's constraints- See Also:
SpringLayout.Constraints
-
getLayoutAlignmentX
public float getLayoutAlignmentX(Container p)
Returns 0.5f (centered).- Specified by:
getLayoutAlignmentX
in interfaceLayoutManager2
-
getLayoutAlignmentY
public float getLayoutAlignmentY(Container p)
Returns 0.5f (centered).- Specified by:
getLayoutAlignmentY
in interfaceLayoutManager2
-
invalidateLayout
public void invalidateLayout(Container p)
Description copied from interface:LayoutManager2
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.- Specified by:
invalidateLayout
in interfaceLayoutManager2
-
putConstraint
public void putConstraint(String e1, Component c1, int pad, String e2, Component c2)
Links edgee1
of componentc1
to edgee2
of componentc2
, with a fixed distance between the edges. This constraint will cause the assignmentvalue(e1, c1) = value(e2, c2) + pad
to take place during all subsequent layout operations.- Parameters:
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 anchor- See Also:
putConstraint(String, Component, Spring, String, Component)
-
putConstraint
public void putConstraint(String e1, Component c1, Spring s, String e2, Component c2)
Links edgee1
of componentc1
to edgee2
of componentc2
. As edge(e2, c2)
changes value, edge(e1, c1)
will be calculated by taking the (spring) sum of(e2, c2)
ands
. Each edge must have one of the following values:SpringLayout.NORTH
,SpringLayout.SOUTH
,SpringLayout.EAST
,SpringLayout.WEST
,SpringLayout.VERTICAL_CENTER
,SpringLayout.HORIZONTAL_CENTER
orSpringLayout.BASELINE
.- Parameters:
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 anchor- See Also:
putConstraint(String, Component, int, String, Component)
,NORTH
,SOUTH
,EAST
,WEST
,VERTICAL_CENTER
,HORIZONTAL_CENTER
,BASELINE
-
getConstraints
public SpringLayout.Constraints getConstraints(Component c)
Returns the constraints for the specified component. Note that, unlike theGridBagLayout
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.- Parameters:
c
- the component whose constraints will be returned- Returns:
- the constraints for the specified component
-
getConstraint
public Spring getConstraint(String edgeName, Component c)
Returns the spring controlling the distance between the specified edge of the component and the top or left edge of its parent. This method, instead of returning the current binding for the edge, returns a proxy that tracks the characteristics of the edge even if the edge is subsequently rebound. Proxies are intended to be used in builder envonments where it is useful to allow the user to define the constraints for a layout in any order. Proxies do, however, provide the means to create cyclic dependencies amongst the constraints of a layout. Such cycles are detected internally bySpringLayout
so that the layout operation always terminates.- Parameters:
edgeName
- must be one ofSpringLayout.NORTH
,SpringLayout.SOUTH
,SpringLayout.EAST
,SpringLayout.WEST
,SpringLayout.VERTICAL_CENTER
,SpringLayout.HORIZONTAL_CENTER
orSpringLayout.BASELINE
c
- the component whose edge spring is desired- Returns:
- a proxy for the spring controlling the distance between the specified edge and the top or left edge of its parent
- See Also:
NORTH
,SOUTH
,EAST
,WEST
,VERTICAL_CENTER
,HORIZONTAL_CENTER
,BASELINE
-
layoutContainer
public void layoutContainer(Container parent)
Description copied from interface:LayoutManager
Lays out the specified container.- Specified by:
layoutContainer
in interfaceLayoutManager
- Parameters:
parent
- the container to be laid out
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-javax/swing/SpringLayout.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.