SpringLayout as Layout Manager in Java

This tutorial is about the SpringLayout as Layout Manager in Java. A SpringLayout is a layout by defining constraints betweem the edges of the components. This attaches springs to other components taht lays out the children of its associated container according to a 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.
  1. import javax.swing.*; //used to access JFrame, JLabel, JTextField, and SpringLayout class
3. Initialize your variables of the components in your Main.
  1. JFrame frame = new JFrame("SpringLayout as Layout Manager");
  2. JLabel label1 = new JLabel("Name: ");
  3. JTextField textField1 = new JTextField("Lyndon Bermoy", 15);
  4. JLabel label2 = new JLabel("Website: ");
  5. JTextField textField2 = new JTextField("www.sourcecodester.com", 15);
  6. 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.
  1. frame.getContentPane().setLayout(layout);
Now, add the components to the frame using the add method.
  1. frame.getContentPane().add(label1);
  2. frame.getContentPane().add(textField1);
  3. frame.getContentPane().add(label2);
  4. frame.getContentPane().add(textField2);
5. Now, put a constraint to the variable layout of SpringLayout where you want to display your components.
  1. // Adjust constraints for the label1 so it's at (10,10).
  2. layout.putConstraint(SpringLayout.WEST, label1, 10, SpringLayout.WEST, frame.getContentPane());
  3. layout.putConstraint(SpringLayout.NORTH, label1, 10, SpringLayout.NORTH, frame.getContentPane());
  4.  
  5. // Adjust constraints for the label2 so it's at (10,30).
  6. layout.putConstraint(SpringLayout.WEST, label2, 10, SpringLayout.WEST,frame.getContentPane());
  7. 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.
  1. // Adjust constraints for the text field so it's at
  2. // (<label1's right edge> + 10, 10).
  3. layout.putConstraint(SpringLayout.WEST, textField1, 10, SpringLayout.EAST, label1);
  4. layout.putConstraint(SpringLayout.NORTH, textField1, 10, SpringLayout.NORTH, frame.getContentPane());
  5.  
  6. // Adjust constraints for the text field so it's at
  7. // (<label2's right edge> + 10, 30).
  8. layout.putConstraint(SpringLayout.WEST, textField2, 10, SpringLayout.EAST, label2);
  9. layout.putConstraint(SpringLayout.NORTH, textField2, 30, SpringLayout.NORTH, frame.getContentPane());
  10.  
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:
  1. // Display the window.
  2. frame.setSize(400,300);
  3. frame.setVisible(true);
  4.  
  5. //close operation
  6. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; //used to access JFrame, JLabel, JTextField, and SpringLayout class
  2.  
  3.  
  4. public class springLayout {
  5.  
  6. public static void main(String[] args) {
  7. JFrame frame = new JFrame("SpringLayout as Layout Manager");
  8. JLabel label1 = new JLabel("Name: ");
  9. JTextField textField1 = new JTextField("Lyndon Bermoy", 15);
  10. JLabel label2 = new JLabel("Website: ");
  11. JTextField textField2 = new JTextField("www.sourcecodester.com", 15);
  12. SpringLayout layout = new SpringLayout();
  13.  
  14. frame.getContentPane().setLayout(layout);
  15.  
  16. frame.getContentPane().add(label1);
  17. frame.getContentPane().add(textField1);
  18. frame.getContentPane().add(label2);
  19. frame.getContentPane().add(textField2);
  20.  
  21. // Adjust constraints for the label1 so it's at (10,10).
  22. layout.putConstraint(SpringLayout.WEST, label1, 10, SpringLayout.WEST, frame.getContentPane());
  23. layout.putConstraint(SpringLayout.NORTH, label1, 10, SpringLayout.NORTH, frame.getContentPane());
  24.  
  25. // Adjust constraints for the text field so it's at
  26. // (<label1's right edge> + 10, 10).
  27. layout.putConstraint(SpringLayout.WEST, textField1, 10, SpringLayout.EAST, label1);
  28. layout.putConstraint(SpringLayout.NORTH, textField1, 10, SpringLayout.NORTH, frame.getContentPane());
  29.  
  30.  
  31. // Adjust constraints for the label2 so it's at (10,30).
  32. layout.putConstraint(SpringLayout.WEST, label2, 10, SpringLayout.WEST,frame.getContentPane());
  33. layout.putConstraint(SpringLayout.NORTH, label2, 30, SpringLayout.NORTH, frame.getContentPane());
  34.  
  35. // Adjust constraints for the text field so it's at
  36. // (<label2's right edge> + 10, 30).
  37. layout.putConstraint(SpringLayout.WEST, textField2, 10, SpringLayout.EAST, label2);
  38. layout.putConstraint(SpringLayout.NORTH, textField2, 30, SpringLayout.NORTH, frame.getContentPane());
  39.  
  40.  
  41. // Display the window.
  42. frame.setSize(400,300);
  43. frame.setVisible(true);
  44.  
  45. //close operation
  46. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47. }
  48. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment