Updated March 17, 2023
Introduction to SpringLayout in Java
In this article, we will learn about Java’s one of the most flexible layout managers, which inherits various features from other layout managers too. In AWT (Abstract Window Toolkit), a SpringLayout Class is responsible for laying out the children to its linked container, based on the set of layout restrictions.
Before understanding SpringLayout, let’s have a look at Spring Framework.
Simply, Spring Framework is an all in one solution to various hurdles. To develop Enterprise Applications Spring Framework uses numerous methods like dependency injection (DI), aspect-oriented programming (AOP) and plain old java object (POJO). Spring is an open-source lightweight framework, that lets Java developers build reliable, scalable and simple enterprise applications. Released in 2004, Spring has had major and significant changes and revisions over the period and as per the need.
Now, SpringLayout, as described earlier, is a very flexible Layout Manager which inherits features of various other layout managers, it was added in JDK version 1.4. To extend, a layout manager is a java object, which implements the LayoutManager interface and is responsible for deciding the position and the size. Spring Layout simply defines the relationship between the edges of components and unlike other layout managers, SpringLayout does not automatically set the location for any of its components. That being said, SpringLayout supports the LayoutManager2 contract accurately. SpringLayout also provides a few solutions to solve problems that cannot be solved by the intervention of the Boxes.
Constructor of SpringLayout in Java
Simply stated, a Java Constructor is a block of program code that initializes an object and as the same name as a class. There are various types of constructors in java. We can overload a constructor for the purpose of creating objects in various ways, and for a Compiler, a Constructor is distinguished based on the number, types, and order of the parameters passed.
public SpringLayout() is a simple Java Constructor which basically constructs a new SpringLayout.
Every single class in Java Programming Language has a constructor, if not created, java implicitly calls a default constructor with values set to zero.
Methods of SpringLayout in Java
Basically, a java method is a collection of program statements, similar to the above-mentioned constructor, with a name specified and can be called/invoked anywhere and anytime in the code. A method can be seen as a subprogram. Unlike a constructor, a method returns a value.
Now, to move further, SpringLayout class provides a wide range of methods, below are the few methods with details.
1. void addLayoutComponent(Component com, Object constraints): Here we have a void type of modifier, and method named addLayoutComponent, which takes two arguments, as string and component.
2. void layoutContainer(Container parent): The same modifier as above, with named as layoutContainer and here parent is the container that has to be laid out.
3. Dimension preferredLayoutSize(Container parent): We have Dimension class here, which sums up the height and the width of a component in a single object. And the parent is the container to be laid out.
4. Dimension minimumLayoutSize(Container parent): With the same Dimension class, minimumLayoutSize simply calculates the minimum size dimensions for a container, the parent is the container to be laid out. SpringLayout also provides maximumLayoutSize, with Dimension class, which returns the maximum dimension size.
5. void removeLayoutComponent(Component component): Same modifier as mentioned earlier, with the name of the component passed as parameter. Here, removeLayoutComponent simply removes the component mentioned in the parameter, from the layout.
6. Spring getConstraint(String edgeName, Component component): getConstraint takes two parameters, one is edgename which must be one of the SpringLayout.EAST, SpringLayout.WEST, SpringLayout.NORTH, SpringLayout.SOUTH, SpringLayout.VERTICAL_CENTER, SpringLayout.HORIZONTAL_CENTER or SpringLayout.BASELINE and the name of the component whose edge spring we wish.
7. void putConstraint(Str e1, Comp c1, int pad, Str e2, Comp c2): This method takes a total of five parameters. Two strings, two components, and a spring. putConstraint simply, connects the component c1’s edge e1 with the component c2’s edge e2, along with a specified distance between edges.
Other than the above-mentioned methods, SpringLayout provides a wide range of methods for various operations, getLayoutAlignmentX and getLayoutAlignmentY returns 0.5f and is a float type.
Simple Java Program Demonstrating SpringLayout in Java
Now that we have understood the whole pf SpringLayout along with its Class and methods, let’s write a simple program to demonstrate a sample Layout. We’ll have a single basic label, a textbox, and a title.
Below is a sample program to demonstrate the working of a SpringLayout Class.
Code:
import java.awt.Component;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
public class SpringSample {
public static void main(String args[]) {
JFrame Sampleframe = new JFrame("Sample SL");
Sampleframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container sampleCP = Sampleframe.getContentPane();
SpringLayout SampleSL = new SpringLayout();
sampleCP.setLayout(SampleSL);
Component left = new JLabel("Sample Label");
Component right = new JTextField(15);
sampleCP.add(left);
sampleCP.add(right);
SampleSL.putConstraint(SpringLayout.NORTH, right, 27, SpringLayout.NORTH, sampleCP);
SampleSL.putConstraint(SpringLayout.NORTH, left, 27, SpringLayout.NORTH, sampleCP);
SampleSL.putConstraint(SpringLayout.WEST, left, 12, SpringLayout.WEST, sampleCP);
SampleSL.putConstraint(SpringLayout.WEST, right, 22, SpringLayout.EAST, left);
Sampleframe.setSize(450, 110);
Sampleframe.setVisible(true);
}
}
Save the above code in a file with .java extension. Compile the java code with javac filename.java and then execute the class file as a java class. Upon executing the code you’ll have the simple output with a specified layout, a single label, and a text box. Refer below screenshot for further understanding.
Output:
Code Interpretation: We initialized with importing required packages. We called JLabel, JTextField, and SpringLayout from javax.swing and two packages from Java Abstract Window Toolkit. We defined a class and the main method inside. Then we defined a frame with “SpringLayout Sample” as a title. We then added a label as in JLabel(“Lable1”), where Label1 is the title of the label and a textbox as JTextField(15) with 15 as an agreement to specify the size of the textbox. Later, we defined SpringLayout based on all directions, along with left and right with contentPane. Finally, we set the frame size with setSize(300, 100) and visibility to true. And now we have a simple SpringLayout with a single Label and a Textbox.
Conclusion
SpringLayout is a java class which sets out the children of its related container, as per the set of restrictions. The object which represents these restrictions has a minimum, maximum, preferred and current value. We understood the SpringLayout class, its constructor and methods and SpringLayout’s Advantages over other Layout Managers.
Recommended Articles
This is a guide to SpringLayout in Java. Here we discuss the constructor and methods of SpringLayout in java along with its code interpretation. You may also look at the following articles to learn more –