Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
SpringLayout is a very flexible layout manager. It maintains the position of GUI’s components with respect to the edges of the container in which they lie or with the edges of other elements of the container. It also inherits many features from other layout managers.
SpringLayout in Java doesn’t set the position of any component automatically. The SpringLayout layout manager sets the position of elements in its container according to the values specified by the developer. Values set by the developer control vertical or horizontal distance between the edges of two components or between the edges of the container.
Constructor of SpringLayout in Java
A constructor is a set of codes that initializes an object.
To declare a new SpringLayout, we use the constructor:
public SpringLayout()
You can also try this code with Online Java Compiler
The field is a member variable of a class that represents a value. Some of the mainly used fields of SpringLayout in Java are described below:
static String BASELINE − This field specifies the baseline of a component.
static String EAST − This field specifies the right edge of the rectangle that bounds an element.
static String HEIGHT − This field specifies the height of the rectangle, framing a UI component of the container.
static String HORIZONTAL_CENTER − This field specifies the horizontal center of the rectangle bounding a UI element of the container.
static String NORTH − This field specifies the top edge of the rectangle framing a UI element of the container.
static String SOUTH − This field specifies the bottom edge of the rectangle bounding a component.
static String VERTICAL_CENTER −This field specifies the vertical center of the rectangle framing a UI element of the container.
static String WEST − This field specifies the left edge of the rectangle bounding a component.
static String WIDTH − This field specifies the width of the rectangle framing a UI element of the container.
Methods of SpringLayout
A method is a set of codes, which can be called anytime and anywhere in the whole execution process. It is a subprogram, and it returns a value. Some of the commonly used methods are described below:
If the control statement is an instance of SpringLayout. Constraints, then associate the control with that particular component.
void
addLayoutComponent(String name, Component c)
This layout manager does not use a per-component string. That’s why it doesn’t have any effect.
Spring
getConstraint(String edgeName, Component c)
This method returns the spring layout manager that controls the distance between the edge of the particular component and the top or left edge of its container.
SpringLayout.Constraints
getConstraints(Component c)
This method returns the constraints for the given particular component.
float
getLayoutAlignmentX(Container p)
This method returns 0.5f. (centered).
float
getLayoutAlignmentY(Container p)
This method returns 0.5f. (centered).
void
invalidateLayout(Container p)
This method invalidates the layout specifying that it should be discarded because the layout manager has cached the information.
void
layoutContainer(Container parent)
This method lays out the given container.
Dimension
maximumLayoutSize(Container parent)
This method calculates the maximum dimension size for the given container.
Dimension
minimumLayoutSize(Container parent)
This method calculates the minimum dimension size for the given container.
Dimension
preferredLayoutSize(Container parent)
This method calculates the preferred dimension size for the given container.
void
removeLayoutComponent(Component c)
This method removes the constraints linked with the particular component.
Methods Inherited
SpringLayout in Java also inherits methods from the following class:
What does “void putConstraint(String e1, Component c1, int pad, String e2, Component c2)” do?
“ void putConstraint(String e1, Component c1, int pad, String e2, Component c2)” Links the edges of components of two different components with fixed space between the edges.
What does “void putConstraint(String e1, Component c1, Spring s, String e2, Component c2)” do?
“void putConstraint(String edg1, Component com1, Spring s, String edg2, Component com2)” Links edge edg1 of component com1 to edge edg2 of component com2.
What does setVisible(true) is used for?
It is a method that lets the frame appear on the screen. If we do not include this in our code, the frame object will exist but will not display on the screen.
What is JButton in Java?
JButton in Java is a graphical element of the user interface which triggers some action when a user clicks on it.
What is JFrame in Java?
JFrame is a container that inherits the properties of Java AWT. It is used in graphics design, and it works like the main window.
Conclusion
In this blog, we came to know about SpringLayout in Java-like: what are its fields, what are the commonly used methods of SpringLayout, methods that it inherits, and also seen an example to implement the SpringLayout in Java.