title: How can I center align a Component with SpringLayout
body: <p>I am trying to create a Form in Java with Swing and I'm having difficulty with managing my layouts.</p>
  
  <p>I want to have a few text fields with labels in the center of a dialog and have "save" and "close" buttons in the lower right.</p>
  
  <p>Adding the buttons to the lower right of the dialog is simple, but I am not sure how to center align the text fields. I figured that, if there wasn't a center component method, then I might be able to align the field by calculating the center of the dialog window and then updating the position when the dialog is re-sized. But I am new to swing and I'm not sure how to do either (or if that is even a good idea).</p>
  
  <p>How can I center align my components using the Spring Layout Manager?</p>
  
  <pre><code>public class Main {
      public static void main(String args[]) {
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  MyFrame myFrame = new MyFrame();
                  myFrame.setVisible(true);
              }
          });
      }
  }
  </code></pre>
  
  <p>Here's how the frame looks:</p>
  
  <pre><code>public class MyFrame extends JFrame {
  
      JLabel label1;
      JTextField field1;
  
      JLabel label2;
      JTextField field2;
  
      JButton saveButton;
      JButton closeButton;
  
      public MyFrame() {
          initLookAndFeel();
          initFrameProperties();
          initContent();
          initLayout();
      }
  
      private initContent() {
          label1= new JLabel("Label 1");
          field1= new JTextField();
          label1.setLabelFor(field1);
  
          label2= new JLabel("Label 2");
          field2= new JTextField();
          label2.setLabelFor(field2);
  
          saveButton = new JButton("Save");
  
          closeButton = new JButton("Close");
          closeButton.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent event) {
                  System.exit(0);
              }
          });
  
  
          this.add(label1);
          this.add(field1);
          this.add(lebel2);
          this.add(field2);
          this.add(saveButton);
          this.add(closeButton);
      }
  
      private void initLayout() {
          SpringLayout layout = new SpringLayout();
  
          this.setLayout(layout);
      }
  </code></pre>