Java AWT | SpringLayout Class

A SpringLayout class in AWT(Abstract Window Toolkit) laid out of the children to its associated container, according to a set of Layout constraints. Each constraint is represented by a Spring object which controls the vertical or horizontal distance between two component edges. The edges can belong to any child of the container, or to the container itself. By default, SpringLayout creates constraints that make their associated component have the minimum, preferred, and maximum and actual value.
Constructor:
- SpringLayout(): Used to constructs a new SpringLayout class.
Commonly Used Methods:
- void addLayoutComponent(Component com, Object cons): If constraints is an instance of SpringLayout.Constraints, associates the constraints with the specified component.
- getLayoutAlignmentX(Container c): Used to returns 0.5f (centered).
- getLayoutAlignmentY(Container c): Used to returns 0.5f (centered).
- getConstraint((String edgeName, Component c): Returns the spring controlling the distance between the specified edge of the component and the top or left edge of its parent.
- getConstraint(Component c): Returns the constraints for the specified component.
- layoutContainer(Container parent): Used to Lays out the specified container.
Below programs illustrate the SpringLayout class:
- Program 1: Below program arranges the components in a JFrame. We create 1 JLabel components named “label” and create a 1 JTextField named “textfield” and create a 2 classes, one is JFrame class and another is SpringLayout class and then add them to the JFrame by the method add(). We set the visibility of the frame using setvisible() method. The layout is set by using setLayout() method.
// Java program to show Example of SpringLayout.// in java. Importing different Package.importjava.awt.Container;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JTextField;importjavax.swing.SpringLayout;// construct a class springdemopublicclassSpringdemo {// It is used to create and show GUIprivatestaticvoidcreateAndShowGUI(){// Creating Object of "JFrame" classJFrame frame =newJFrame("MySpringDemp");// Function to set default// close operation of JFrame.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// to get content paneContainer contentPane = frame.getContentPane();// Creating Object of "Springlayout" classSpringLayout layout =newSpringLayout();// to set content panecontentPane.setLayout(layout);// Initialization of object// "label" of JLabel class.JLabel label =newJLabel("Label: ");// Initialization of object// "label" of JLabel class.JTextField textField =newJTextField("Enter the text ",15);// to add content pane of JLabelcontentPane.add(label);// to add content pane of JTextFieldcontentPane.add(textField);// It is used to put the layout// constraint in JFrame using springLayout classlayout.putConstraint(SpringLayout.WEST, label,6, SpringLayout.WEST, contentPane);layout.putConstraint(SpringLayout.NORTH, label,6, SpringLayout.NORTH, contentPane);layout.putConstraint(SpringLayout.WEST, textField,6, SpringLayout.EAST, label);layout.putConstraint(SpringLayout.NORTH, textField,6, SpringLayout.NORTH, contentPane);layout.putConstraint(SpringLayout.EAST, contentPane,6, SpringLayout.EAST, textField);layout.putConstraint(SpringLayout.SOUTH, contentPane,6, SpringLayout.SOUTH, textField);// Function to pack the JFrame.frame.pack();// Function to set visible status of JFrame.frame.setVisible(true);}// Main Methodpublicstaticvoidmain(String[] args){javax.swing.SwingUtilities.invokeLater(newRunnable(){// create a classpublicvoidrun(){// to create and show GUIcreateAndShowGUI();}});}}Output:
- Program 2: Below program arranges the components in a JFrame. We create 1 class named “springlayout class” and create a 4 JButton components named “b1″, “b2”, “b3”, “b4”, “b5” and then add them to the JFrame by the method add(). We set the visibility of the frame by using the method setvisible(). The layout is set by setLayout() method.
// Java program to show Example of SpringLayout.// in java. Importing different Package.importjava.awt.*;importjavax.swing.*;// construct a class SpringclassdemopublicclassSpringclassdemo {// Main Methodpublicstaticvoidmain(String[] arguments){// main window// Function to set the default look// and feel decorated status of JFrame.JFrame.setDefaultLookAndFeelDecorated(true);// Creating Object of "JFrame" classJFrame frame =newJFrame("SpringLayoutExample Example");// Function to set the default// close operation status of JFrameframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Function to set the// size status of JFrameframe.setSize(300,200);// to get the content paneContainer content = frame.getContentPane();// Creating Object of "SpringLayout" classSpringLayout layout =newSpringLayout();// to set the layout classframe.setLayout(layout);// Initialization of object// "b1" of JButton class.Component b1 =newJButton("GEEKS");// Initialization of object// "b2" of JButton class.Component b2 =newJButton("GFG");// Initialization of object// "b3" of JButton class.Component b3 =newJButton("JAVA");// Initialization of object// "b4" of JButton class.Component b4 =newJButton("Sudo Placement");// Adding the JButton "b1" on frameframe.add(b1);// Adding the JButton "b2" on frameframe.add(b2);// Adding the JButton "b3" on frameframe.add(b3);// Adding the JButton "b4" on frameframe.add(b4);// It is used to put the layout// constraint in JFrame using// springLayout class on b1 JButtonlayout.putConstraint(SpringLayout.WEST, b1,25, SpringLayout.WEST, content);layout.putConstraint(SpringLayout.NORTH, b1,10, SpringLayout.NORTH, content);// It is used to put the layout// constraint in JFrame using// springLayout class on b2 JButtonlayout.putConstraint(SpringLayout.WEST, b2,50, SpringLayout.WEST, content);layout.putConstraint(SpringLayout.NORTH, b2,10, SpringLayout.SOUTH, b1);// It is used to put the layout// constraint in JFrame using// springLayout class on b3 JButtonlayout.putConstraint(SpringLayout.WEST, b3,75, SpringLayout.WEST, content);layout.putConstraint(SpringLayout.NORTH, b3,10, SpringLayout.SOUTH, b2);// It is used to put the layout// constraint in JFrame using// springLayout class on b4 JButtonlayout.putConstraint(SpringLayout.WEST, b4,15, SpringLayout.EAST, b1);layout.putConstraint(SpringLayout.NORTH, b4,10, SpringLayout.NORTH, content);// Function to set the// visible status of JFrameframe.setVisible(true);}}Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/javax/swing/SpringLayout.html



