Merlin's Magic: Springlayout Manager

Source: Internet
Author: User
Tags constant

In the Java Layout manager, the latest addition to the Springlayout Manager is published with Java 1.4. This layout manager allows you to attach "spring" to components so that the components can be laid out relative to other components. For example, you can use Springlayout to display a button attached to the right border, regardless of how large the screen width is.

Start using Springlayout

As with all layout managers, the Springlayout manager is responsible for arranging the location of the components. The location of the component is controlled by providing an associated constraint. For Springlayout-controlled components, there is a constraint with 4 set values-each setting value corresponds to each edge of the component. The Springlayout manager relies on a Springlayout.constraints object to provide these component constraints. This is similar to how the Gridbagconstraints class works, and the Gridbagconstraints class complements the GridBagLayout Manager: Each component added to the container can have an attached Springlayout.constraints object. However, the similarities between the two are just that.

When using GridBagLayout, your usual practice is to add components to the container by using constraints. In the case of using the Springlayout Manager, you typically do not have to use constraints to add components. Instead, you can add the component directly, and then attach the constraint separately. In addition to springlayout, nothing can prevent you from adding constraints to your component. Constraints is not a simple class. It is a collection of Spring objects (for each edge). When you use the Springlayout.constraints class, you need to add each Spring constraint separately to springlayout.constraints. You add a constraint to the springlayout.constraints by setting a specific constraint on one of the component's edges. You can call Springlayout.constraints's setcontraints by using the 4 Springlayout constants of East, west, north, and SOUTH (String edge, Spring Spring) method, where the String type argument is one of 4 constants. For example, if you want to add a component to the top left side of a container, you can create two sized spring, combine them, and then add the component to a container with this combination of spring sets, as shown in Listing 1:

Listing 1. Using Springlayout

Component left = ...;
SpringLayout layout = new SpringLayout();
JPanel panel = new JPanel(layout);
Spring xPad = Spring.constant(5);
Spring yPad = Spring.constant(25);
SpringLayout.Constraints constraint = new SpringLayout.Constraints();
constraint.setConstraint(SpringLayout.WEST, xPad);
constraint.setConstraint(SpringLayout.NORTH, yPad);
contentPane.add(left, constraint);

This may not seem particularly difficult, but when you need to add the next component, it's a little trickier to add to the right or bottom of the first component. You cannot simply add components outside of n pixels; in fact, you must add padding (filler) to earlier components. To find the edge of the previous component, you can use the Getconstraint () method to request the layout manager to pass the edges and components that you are targeting for the method, such as Layout.getconstraint (Springlayout.east, left). To get the position of the correct side of the first component. From this location, you can add the required padding and attach it to the edges of the other components, as shown in Listing 2:

Listing 2. Add a second component using Springlayout

Component right = ...;
Spring rightSideOfLeft = layout.getConstraint(SpringLayout.EAST, left);
Spring pad = Spring.constant(20);
Spring leftEdgeOfRight = Spring.sum(rightSideOfLeft, pad);
constraint = new SpringLayout.Constraints();
constraint.setConstraint(SpringLayout.WEST, leftEdgeOfRight);
constraint.setConstraint(SpringLayout.NORTH, yPad);
contentPane.add(right, constraint);

Use Putconstraint () via Springlayout

This approach works very well, but if the number of components is more numerous, this approach is tedious. Instead, another way to circumvent the intermediate step is to add the component directly without constraint and then add the constraint separately, using the Springlayout Putconstraint () method to connect the constraint to the component, as shown in Listing 3:

Listing 3. Add a second component with Springlayout

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.