Java Swing Tutorial - Java Swing SpringLayout








An instance of the SpringLayout class in the javax.swing package represents a SpringLayout manager.

A spring in the context of a SpringLayout manager can be stretched, compressed, or stay in its normal state.

A Spring object has four properties: minimum, preferred, maximum, and current value.

A spring has its minimum value when it is most compressed. In its normal state, it has its preferred value.

In its most stretched state, it has its maximum value. Its value at any given point in time is its current value.

When the minimum, preferred, and maximum values of a spring are the same, it is known as a strut.

We can create a spring using the factory methods.

To create a spring or strut from scratch, we can use its overloaded constant() static method.

We can also create a spring using the width or height of a component.

To create a strut of 10 pixels

Spring strutPadding  = Spring.constant(10);

To create a spring having 10, 25 and 50 as its minimum, preferred, and maximum value respectively.

Spring springPadding = Spring.constant(10, 25,   50);

To create a spring from the width of a component named c1

Spring s1  = Spring.width(c1);

To create a spring from the height of a component named c1

Spring s2  = Spring.height(c1);

The Spring class has some utility methods that let we manipulate spring properties.

The following code can create a new spring by adding two springs using the sum() method.

Spring s3  = Spring.sum(s1, s2);

We can create a spring by subtracting one spring from another with minus() method.

To get the maximum of two springs s1 and s2, we can use Spring.max(s1, s2).

To create a fraction of another spring using the scale() method.

String fractionSpring = Spring.scale(s1, 0.40f);
import java.awt.Container;
/*  ww  w .  j a  va 2  s  .  c  o m*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SpringLayout;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("SpringLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    // Set the content pane's layout as SpringLayout
    SpringLayout springLayout = new SpringLayout();
    contentPane.setLayout(springLayout);

    // Add two JButtons to the content pane
    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Little Bigger   Button   2");
    contentPane.add(b1);
    contentPane.add(b2);

    frame.pack();
    frame.setVisible(true);
  }
}




Constraints

The following table lists of Constants Defined in the SpringLayout Class.

Constant NameDescription
NORTHIt is synonymous with y. It is the top edge of the component.
WESTIt is synonymous with x. It is the left edge of the component.
SOUTHIt is the bottom edge of the component. Its value is the same as NORTH + HEIGHT.
EASTIt is the right edge of the component. It is the same as WEST + WIDTH.
WIDTHThe width of the component.
HEIGHTThe height of the component.
HORIZONTAL_CENTERIt is the horizontal center of the component. It is the same as WEST + WIDTH/2.
VERTICAL_CENTERIt is the vertical center of the component. It is the same as NORTH + HEIGHT/2.
BASELINEIt is the baseline of the component.

The following code shows how to set the x and y constraints of a component relative to the container or to another component.

import java.awt.Container;
//  w  w  w  . j  ava 2s.  co m
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Spring;
import javax.swing.SpringLayout;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("SpringLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    // Set the content pane's layout to a SpringLayout
    SpringLayout springLayout = new SpringLayout();
    contentPane.setLayout(springLayout);

    // Add two JButtons to the content pane
    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Button Second");

    // Create Constraints objects for b1 and b2
    SpringLayout.Constraints b1c = new SpringLayout.Constraints();
    SpringLayout.Constraints b2c = new SpringLayout.Constraints();

    // Create a Spring object for y value for b1 and b2
    Spring yPadding = Spring.constant(20);

    // Set (10, 20) for (x, y) for b1
    b1c.setX(Spring.constant(10));
    b1c.setY(yPadding);

    // Set (150, 20) for (x, y) for b2
    b2c.setX(Spring.constant(150));
    b2c.setY(yPadding);

    // Use the Constraints object while adding b1 and b2
    contentPane.add(b1, b1c);
    contentPane.add(b2, b2c);

    frame.pack();
    frame.setVisible(true);
  }
}

To set the constraints for components in a SpringLayout we can also use the putConstraint() method of the SpringLayout class.

Here are two versions of the putConstraint() method:

void  putConstraint(String targetEdge, Component targetComponent, int  padding, String sourceEdge,Component sourceComponent)

void  putConstraint(String targetEdge, Component targetComponent, Spring padding, String  sourceEdge, Component sourceComponent)

The first version uses a strut. int padding defines a fixed spring, which will behave as a strut (a fixed distance) between the edges of two components.

The second version uses a spring instead.

The position the left edge of b2 to be 5 pixels from the right edge of b1, use the following method.

springLayout.putConstraint(SpringLayout.WEST, b2,   5, SpringLayout.EAST, b1);

To set the left edge of b1 10 pixels from the left edge of the content pane.

springLayout.putConstraint(SpringLayout.WEST, b1,   5, SpringLayout.WEST,   contentPane);




Example

The following code shows how to use the putConstraint() Method of the SpringLayout Class.

import java.awt.Container;
/*  www .  j av  a  2s  . c o  m*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SpringLayout;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("SpringLayout2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    SpringLayout springLayout = new SpringLayout();
    contentPane.setLayout(springLayout);

    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Button Second");

    contentPane.add(b1);
    contentPane.add(b2);

    springLayout.putConstraint(SpringLayout.WEST, b1, 10, SpringLayout.WEST, contentPane);
    springLayout.putConstraint(SpringLayout.NORTH, b1, 20, SpringLayout.NORTH, contentPane);

    springLayout.putConstraint(SpringLayout.WEST, b2, 10, SpringLayout.EAST, b1);

    springLayout.putConstraint(SpringLayout.NORTH, b2, 20,SpringLayout.NORTH, contentPane);

    springLayout.putConstraint(SpringLayout.SOUTH, contentPane, 10,SpringLayout.SOUTH, b1);

    springLayout.putConstraint(SpringLayout.EAST, contentPane, 10, SpringLayout.EAST, b2);

    frame.pack();
    frame.setVisible(true);
  }
}