Java Reference
In-Depth Information
To set the width and height of the component, you call the
setWidth()
and
setHeight()
methods for
its
SpringLayout.Constraints
object and supply a
Spring
object that you want to control the dimension.
Here's how you could specify specific
Spring
objects of your choosing:
Spring wSpring = Spring.constant(30,50,70); // Spring we'll use for width
Spring hSpring = Spring.constant(15); // Strut we'll use for height
constraints.setWidth(wSpring); // Set component width constraint
constraints.setHeight(hSpring); // Set component height constraint
The width constraint is applied between the
WEST
and
EAST
edges and the height constraint applies
between the component's
NORTH
and
SOUTH
edges. Because you have specified a strut for the height, there is
no leeway on this constraint; its value is fixed at 15.
The
Spring
class defines the static methods
width()
and
height()
that return a
Spring
object based on
the minimum, maximum, and preferred sizes of the component that you pass as the argument to the method.
These are the constraints that the layout manager applies to the width and height by default, but there may
be circumstances where you want to reapply them. You could use these methods to create
Spring
objects to
control the width and height of the
button
object like this:
constraints.setWidth(Spring.width(button)); // Set component width constraint
constraints.setHeight(Spring.width(button)); // Set component height constraint
The springs you have set here adjust the width and height based on the component's minimum, maxim-
um, and preferred sizes.
If you want to explicitly set an edge constraint for a component, you call the
setConstraint()
method
for the component's
SpringLayout.Constraints
object:
layout.getConstraints(newButton).setConstraint(
StringLayout.EAST, Spring.sum(xSpring, wSpring));