Java Reference
In-Depth Information
ponent. Normally these constraints on the width and height expand or contract the component as the size
of the container changes, but when the getPreferredSize() and getMaximumSize() methods return the
same value, the layout manager does not alter the width and height of the component as the container size is
changed.
Defining Spring Constraints
The Spring class in the javax.swing package defines an object that represents a constraint. A Spring ob-
ject is defined by three integer values that relate to the notional length of the spring: the minimum length, the
preferred length, and the maximum length. A Spring object also has an actual length value that lies between
the minimum and the maximum, and that determines the location of the edge to which it applies. You can
create a Spring object like this:
Spring spring = Spring.constant(10, 30, 50); // min=10, pref=30, max=50
The static constant() method in the Spring class creates a Spring object from the three arguments that
are the minimum, preferred, and maximum values for the Spring object. If all three values are equal, the
object is called a strut because its value is fixed at the common value you set for all three. There's an over-
loaded version of the constant() method for creating struts that has just one parameter:
Spring strut = Spring.constant(40); // min, pref, and max all set to 40
The Spring class also defines static methods that operate on Spring objects:
Spring sum(Spring spr1, Spring spr2) : Returns a reference to a new object that has min-
imum, preferred, and maximum values that are the sum of the corresponding values of the argu-
ments.
Spring minus(Spring spr) : Returns a reference to a new object with minimum, preferred, and
maximum values that are the same magnitude as those of the argument but with opposite signs.
Spring max(Spring spr1,Spring spr2) : Returns a reference to a new object that has min-
imum, preferred, and maximum values that are the maximum of the corresponding values of the
arguments.
Setting Constraints for a Component
The setX() and setY() methods for a SpringLayout.Constraints object set the constraints for the WEST
and NORTH edges of the component, respectively. For example:
SpringLayout.Constraints constraints = layout.getConstraints(button);
Spring xSpring = Spring.constant(5,10,20); // Spring we'll use for X
Spring ySpring = Spring.constant(3,5,8); // Spring we'll use for Y
constraints.setX(xSpring); // Set the WEST edge constraint
constraints.setY(xSpring); // Set the NORTH edge constraint
The layout variable references a SpringLayout object that has been set as the layout manager for the
container that contains the button component. The setX() method defines a constraint between the WEST
edge of the container and the WEST edge of the button component. Similarly, the setY() method defines a
constraint between the NORTH edge of the container and the NORTH edge of the button component. This fixes
the location of the component relative to the origin of the container, as illustrated in Figure 17-34 .
 
Search WWH ::




Custom Search