SpringLayout as Layout Manager in Java

May 28, 2020
Tutorials
springlayout in java

This instructional exercise is about the SpringLayout as Layout Manager in Java. A SpringLayout is a format by defining constraints between the edges of the parts. This attached springs to different parts that layout the offspring of its associated container as per the set of constraints.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of springLayout.java.

2. Import javax.swing.* package library to access JFrame, JLabel, JTextField, and SpringLayout class.

import javax.swing.*; //used to access JFrame, JLabel, JTextField, and SpringLayout class

 

3. Initialize your variables of the components in your Main.

JFrame frame = new JFrame("SpringLayout as Layout Manager");
JLabel label1 = new JLabel("Name: ");
JTextField textField1 = new JTextField("Lyndon Bermoy", 15);
JLabel label2 = new JLabel("Website: ");
JTextField textField2 = new JTextField("www.sourcecodester.com", 15);
SpringLayout layout = new SpringLayout();

 

We have created two JTextFields named textField1 and textField2 and two JLabels named label1 and label2. We initialize the SpringLayout named layout variable.

4. Make your layout to SpringLayout using the setLayout method.

frame.getContentPane().setLayout(layout);

Now, add the components to the frame using the add method.

frame.getContentPane().add(label1);
frame.getContentPane().add(textField1);
frame.getContentPane().add(label2);
frame.getContentPane().add(textField2);

 

5. Now, put a constraint to the variable layout of SpringLayout where you want to display your components.

// Adjust constraints for the label1 so it's at (10,10).
layout.putConstraint(SpringLayout.WEST, label1, 10, SpringLayout.WEST, frame.getContentPane());
layout.putConstraint(SpringLayout.NORTH, label1, 10, SpringLayout.NORTH, frame.getContentPane());
 
// Adjust constraints for the label2 so it's at (10,30).
layout.putConstraint(SpringLayout.WEST, label2, 10, SpringLayout.WEST,frame.getContentPane());
layout.putConstraint(SpringLayout.NORTH, label2, 30, SpringLayout.NORTH, frame.getContentPane());

 

The first putConstraint in label1 specifies that the label’s left west position will be 10 in the x position from the frame’s left edge. The second putConstraint call sets up to the north position of 10 initiating the y coordinate.

The label2 sets up its constraint to 10 as x-coordinate and 30 as the y-coordinate.

// Adjust constraints for the text field so it's at
// (<label1's right edge> + 10, 10).
layout.putConstraint(SpringLayout.WEST, textField1, 10, SpringLayout.EAST, label1);
layout.putConstraint(SpringLayout.NORTH, textField1, 10, SpringLayout.NORTH, frame.getContentPane());
 
// Adjust constraints for the text field so it's at
// (<label2's right edge> + 10, 30).
layout.putConstraint(SpringLayout.WEST, textField2, 10, SpringLayout.EAST, label2);
layout.putConstraint(SpringLayout.NORTH, textField2, 30, SpringLayout.NORTH, frame.getContentPane());

 

The first putConstraint in textField1 specifies that the label’s left west position will be added to the 10 as x-coordinate. The second putConstraint call sets up to the north position of 30 initiating the y coordinate.

6. Lastly, set its size and visibility, and its close operation. Have this code below:

// Display the window.
frame.setSize(400,300);
frame.setVisible(true);
 
//close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

Output:

springlayout in java

Here’s the full code of this tutorial:

import javax.swing.*; //used to access JFrame, JLabel, JTextField, and SpringLayout class
 
 
public class springLayout {
 
  public static void main(String[] args) {
    JFrame frame = new JFrame("SpringLayout as Layout Manager");
    JLabel label1 = new JLabel("Name: ");
    JTextField textField1 = new JTextField("Lyndon Bermoy", 15);
    JLabel label2 = new JLabel("Website: ");
    JTextField textField2 = new JTextField("www.sourcecodester.com", 15);
    SpringLayout layout = new SpringLayout();
 
    frame.getContentPane().setLayout(layout);
 
    frame.getContentPane().add(label1);
    frame.getContentPane().add(textField1);
    frame.getContentPane().add(label2);
    frame.getContentPane().add(textField2);
 
    // Adjust constraints for the label1 so it's at (10,10).
    layout.putConstraint(SpringLayout.WEST, label1, 10, SpringLayout.WEST, frame.getContentPane());
    layout.putConstraint(SpringLayout.NORTH, label1, 10, SpringLayout.NORTH, frame.getContentPane());
 
    // Adjust constraints for the text field so it's at
    // (<label1's right edge> + 10, 10).
    layout.putConstraint(SpringLayout.WEST, textField1, 10, SpringLayout.EAST, label1);
    layout.putConstraint(SpringLayout.NORTH, textField1, 10, SpringLayout.NORTH, frame.getContentPane());
 
 
    // Adjust constraints for the label2 so it's at (10,30).
    layout.putConstraint(SpringLayout.WEST, label2, 10, SpringLayout.WEST,frame.getContentPane());
    layout.putConstraint(SpringLayout.NORTH, label2, 30, SpringLayout.NORTH, frame.getContentPane());
 
    // Adjust constraints for the text field so it's at
    // (<label2's right edge> + 10, 30).
    layout.putConstraint(SpringLayout.WEST, textField2, 10, SpringLayout.EAST, label2);
    layout.putConstraint(SpringLayout.NORTH, textField2, 30, SpringLayout.NORTH, frame.getContentPane());
 
 
    // Display the window.
    frame.setSize(400,300);
    frame.setVisible(true);
 
    //close operation
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

 

 

Facebook Comments

Leave a Reply

Your email address will not be published.