ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

JFC Swing: The SpringLayout Class
Pages: 1, 2, 3, 4

The Spring Class

So what are these Spring objects we keep seeing everywhere? As stated earlier, they are essentially a collection of three values: a minimum, a maximum, and a preferred value. You could use a spring to describe the height property of a text area, for example. Its minimum is 25 pixels; its maximum is the height of the screen (say, 1024), and its preferred height is 8 rows, or 200 pixels. By the same token, you can create a strut by specifying a spring with identical values for all three properties; for example, a text field with 25 for its minimum, maximum, and preferred heights.

Beyond the basic expandability of a spring, you can do some fun things with them. Springs can be manipulated using mathematical concepts (and achieve a semantically-correct result). For example, you can add two springs together. The new spring consists of the sum of the minimums, the sum of the preferred values, and the sum of the preferred maximums. You can also negate a spring and effectively multiply each of its values by -1. Summing with a negated spring, then, becomes a difference operation.

Now here's where things get really interesting. When you sum two springs, the result is not calculated immediately. The resulting spring stores a reference to the two operand springs. When needed, the summed spring queries its sub-springs. This has the practical upshot of making springs dynamic. If you change one spring in a sum, the sum changes, too. This turns out to be very useful in layout managers. Attach a spring to the bottom of the container, and it stretches whenever the container stretches.

Consider the y property for the South button in the application shown in Figure 2. We can use subtraction to keep the button about 50 pixels above the bottom of the frame, even after the frame has been resized.

Spring offset = Spring.sum(buttonHeight, Spring.constant(50));
Spring southY = Spring.sum(bottom, Spring.minus(offset));

Figure 3 shows the details of each spring behind this button. (You can refer back to the code for the syntax on creating all of the springs you see in the diagram.)

Figure 3. Constructing the x, y, width, and height springs (and struts) for the South button.


Only one constant is defined for the Spring class, as shown in Table 3.

Table 3. Spring Constants.

Constant Type Description
UNSET int An indicator that this property has not yet been calculated. (It's internal value is Integer.MIN_VALUE.) Can be used as a notification to recalculate any cached values.


The properties for Spring, shown in Table 4, are fairly straightforward. In addition to the minimum, maximum, and preferred properties, you can read and write the current value of the spring. Notice that the minimum, maximum, and preferred properties are read-only. You can extend Spring if you need to have a more dynamic spring.

Table 4. Spring Properties.

Property Data Type get is set Default Value
maximum int *     UNSET
minimum int *     UNSET
preferred int *     UNSET
value int *   * UNSET

Creating Springs

The default constructor for Spring is protected. It's obviously meant for subclasses. To create a spring, you use the factory-style constant() methods.

The following method creates a strut. The minimum, preferred, and maximum properties are all set to pref.

public static Spring constant(int pref)

The following method creates a spring. The minimum, preferred, and maximum properties are set from min, pref, and max, respectively.

public static Spring constant(int min, int pref, int max)

Pages: 1, 2, 3, 4

Next Pagearrow