javax.swing

Class SpringLayout

public class SpringLayout extends Object implements LayoutManager2

A very flexible layout manager. Components are laid out by defining the relationships between them. The relationships are expressed as {@link Spring}s. You can attach a Spring for each edge of a component and link it to an edge of a different component. For example, you can say, the northern edge of component A should be attached to the southern edge of component B, and the space between them should be something between x and y pixels, and preferably z pixels.

While quite simple, this layout manager can be used to emulate most other layout managers, and can also be used to solve some layout problems, which would be hard to solve with other layout managers.

Nested Class Summary
static classSpringLayout.Constraints
The constraints that define the relationships between components.
Field Summary
static StringEAST
The right edge of a component.
static StringNORTH
The top edge of a component.
static StringSOUTH
The bottom edge of a component.
static StringWEST
The left edge of a component.
Constructor Summary
SpringLayout()
Creates a new SpringLayout.
Method Summary
voidaddLayoutComponent(Component component, Object constraint)
Adds a layout component and a constraint object to this layout.
voidaddLayoutComponent(String name, Component c)
Adds a layout component and a constraint object to this layout.
SpringgetConstraint(String edgeName, Component c)
Returns the constraint of the edge named by edgeName.
SpringLayout.ConstraintsgetConstraints(Component c)
Returns the {@link Constraints} object associated with the specified component.
floatgetLayoutAlignmentX(Container p)
Returns the X alignment of the Container p.
floatgetLayoutAlignmentY(Container p)
Returns the Y alignment of the Container p.
voidinvalidateLayout(Container p)
Recalculate a possibly cached layout.
voidlayoutContainer(Container p)
Lays out the container p.
DimensionmaximumLayoutSize(Container p)
Calculates the maximum size of the layed out container.
DimensionminimumLayoutSize(Container p)
Calculates the minimum size of the layed out container.
DimensionpreferredLayoutSize(Container p)
Calculates the preferred size of the layed out container.
voidputConstraint(String e1, Component c1, int pad, String e2, Component c2)
Attaches the edge e1 of component c1 to the edge e2 of component c2 width the fixed strut pad.
voidputConstraint(String e1, Component c1, Spring s, String e2, Component c2)
Attaches the edge e1 of component c1 to the edge e2 of component c2 width the {@link Spring} s.
voidremoveLayoutComponent(Component c)
Removes a layout component.

Field Detail

EAST

public static final String EAST
The right edge of a component.

NORTH

public static final String NORTH
The top edge of a component.

SOUTH

public static final String SOUTH
The bottom edge of a component.

WEST

public static final String WEST
The left edge of a component.

Constructor Detail

SpringLayout

public SpringLayout()
Creates a new SpringLayout.

Method Detail

addLayoutComponent

public void addLayoutComponent(Component component, Object constraint)
Adds a layout component and a constraint object to this layout. This method is usually only called by a {@link java.awt.Container}s add method.

Parameters: component the component to be added. constraint the constraint to be set.

addLayoutComponent

public void addLayoutComponent(String name, Component c)
Adds a layout component and a constraint object to this layout. This method is usually only called by a {@link java.awt.Container}s add method. This method does nothing, since SpringLayout does not manage String-indexed components.

Parameters: name the name. c the component to be added.

getConstraint

public Spring getConstraint(String edgeName, Component c)
Returns the constraint of the edge named by edgeName.

Parameters: c the component from which to get the constraint. edgeName the name of the edge, one of {@link #EAST}, {@link #WEST}, {@link #NORTH} or {@link #SOUTH}.

Returns: the constraint of the edge edgeName of the component c.

getConstraints

public SpringLayout.Constraints getConstraints(Component c)
Returns the {@link Constraints} object associated with the specified component.

Parameters: c the component for which to determine the constraint.

Returns: the {@link Constraints} object associated with the specified component.

getLayoutAlignmentX

public float getLayoutAlignmentX(Container p)
Returns the X alignment of the Container p.

Parameters: p the {@link java.awt.Container} for which to determine the X alignment.

Returns: always 0.0

getLayoutAlignmentY

public float getLayoutAlignmentY(Container p)
Returns the Y alignment of the Container p.

Parameters: p the {@link java.awt.Container} for which to determine the Y alignment.

Returns: always 0.0

invalidateLayout

public void invalidateLayout(Container p)
Recalculate a possibly cached layout.

layoutContainer

public void layoutContainer(Container p)
Lays out the container p.

Parameters: p the container to be laid out.

maximumLayoutSize

public Dimension maximumLayoutSize(Container p)
Calculates the maximum size of the layed out container. This respects the maximum sizes of all contained components.

Parameters: p the container to be laid out.

Returns: the maximum size of the container.

minimumLayoutSize

public Dimension minimumLayoutSize(Container p)
Calculates the minimum size of the layed out container. This respects the minimum sizes of all contained components.

Parameters: p the container to be laid out.

Returns: the minimum size of the container.

preferredLayoutSize

public Dimension preferredLayoutSize(Container p)
Calculates the preferred size of the layed out container. This respects the preferred sizes of all contained components.

Parameters: p the container to be laid out.

Returns: the preferred size of the container.

putConstraint

public void putConstraint(String e1, Component c1, int pad, String e2, Component c2)
Attaches the edge e1 of component c1 to the edge e2 of component c2 width the fixed strut pad.

Parameters: e1 the edge of component 1. c1 the component 1. pad the space between the components in pixels. e2 the edge of component 2. c2 the component 2.

putConstraint

public void putConstraint(String e1, Component c1, Spring s, String e2, Component c2)
Attaches the edge e1 of component c1 to the edge e2 of component c2 width the {@link Spring} s.

Parameters: e1 the edge of component 1. c1 the component 1. s the space between the components as a {@link Spring} object. e2 the edge of component 2. c2 the component 2.

removeLayoutComponent

public void removeLayoutComponent(Component c)
Removes a layout component.

Parameters: c the layout component to remove.