The only constructor for
SpringLayout is the default constructor. Similar to the way one uses
CardLayout, you'll want to keep a reference to your
SpringLayout manager handy.
Create a new
As with other layout managers, a majority of the methods in
SpringLayout are devoted to meeting the contract of the
LayoutManager2 interfaces. The methods that make this manager interesting, however, are the methods dealing with components' constraints.
The following method returns the entire set of constraints (the springs on all four edges) for the given component. We discuss the
Constraints inner class in the next section.
public SpringLayout.Constraints getConstraints(Component c)
The following method returns a particular spring for the specified edge (
edgeName) of the given component (
public Spring getConstraint(String edgeName, Component c)
These methods place a constraint (spring) between two edges. The first method is just a convenience method that uses
pad to create a strut.
c1 are associated with the dependent component, while
c2 refer to the anchor.
public void putConstraint(String e1, Component c1, int pad, String e2, Component c2) public void putConstraint(String e1, Component c1, Spring s, String e2, Component c2)
No method exists for setting all of the constraints at one time, but you can use the object returned by the
getConstraints() method to manipulate all of the edges on a component.
Here's the source code for the example application shown in Figure 2. Notice the three primary means of positioning components in a
SpringLayout. We add the North and South buttons to the container with prebuilt constraints. The East and West buttons are positioned by retrieving their existing constraints and setting up the bounding box for the component. For example, the North and East buttons are set up like this:
// Add north button c.add(nb, new SpringLayout.Constraints(northX, offsetS, widthS, heightS)); // Add east button c.add(eb); sl.getConstraints(eb).setX(eastX); sl.getConstraints(eb).setY(eastY); sl.getConstraints(eb).setWidth(widthS); sl.getConstraints(eb).setHeight(heightS);
As an example of the third mechanism for positioning components, the viewport for the graphics image is laid out using several
c.add(viewport); // The order here is important...need to have a valid width and height // in place before binding the (x,y) location sl.putConstraint(SpringLayout.SOUTH, viewport, Spring.minus(borderS), SpringLayout.SOUTH, c); sl.putConstraint(SpringLayout.EAST, viewport, Spring.minus(borderS), SpringLayout.EAST, c); sl.putConstraint(SpringLayout.NORTH, viewport, topBorder, SpringLayout.NORTH, c); sl.putConstraint(SpringLayout.WEST, viewport, leftBorder, SpringLayout.WEST, c);
You might notice some funky springs in this example. We'll explain the centering spring and the
minus() methods in the section on the
Spring class itself.
The Constraints Inner Class
SpringLayout.Constraints embodies the spring constraints placed on a single component in a
SpringLayout-managed container. It holds the bounding box for a component, but it uses
Spring references rather than
ints for the
Constraints inner class consists entirely of the properties shown in Table 2. With the exception of the
constraint property, these properties mimic the
Rectangle class often used to describe the bounds of components. The
constraint property is indexed by edge name (a
String). See Table 1, SpringLayout Constants, for a list of valid edge names and their respective relationships to the
Table 2. SpringLayout.Constraints Properties.
|Property||Data Type||get||is||set||Default Value|
i is indexed (by String values, see Table 1).
There are several constructors for building a
Constraints object. You might want to do this if you intend to build the constraints before adding the component to its container. (You can use the
Container.add(Component, Object) method to accomplish this.)
public SpringLayout.Constraints() public SpringLayout.Constraints(Spring x, Spring y) public SpringLayout.Constraints(Spring x, Spring y, Spring width, Spring height)
These constructors all build
Constraints objects. Any unspecified property is left as a null value. The first constructor creates a completely empty
Constraints object, while the second one leaves the
height properties null.