title: Align 2 sets/items on either side of center
body: <p>I'm trying to figure out the best way to align 2 sets of items in the center of a panel in a Java Swing application.  The panel is in the <code>North</code> position of a <code>BorderLayout</code> whose width is determined by the <code>JTextField</code> in the <code>Center</code> position of the layout.  The problem I'm having is, I have a set of labels and smaller text fields that I want to center so that the end of the label and the start of the first text field meet at the center of the panel.</p>
  
  <p>I've tried <code>GroupLayout</code>, but ended up with the following result:
  <img src="https://i.stack.imgur.com/1qOyZ.jpg" alt="enter image description here"></p>
  
  <p>Note: <em>The 2 text fields separated by a + are in a sub-panel.</em></p>
  
  <p>What I'm trying to achieve is the following:
  <img src="https://i.stack.imgur.com/y0phR.jpg" alt="enter image description here"></p>
  
  <p>Apparently I'm either missing something, or this is far more complicated than necessary to do.  I actually run into this issue a LOT!  I'm surprised there isn't a special grid layout specifically for this.</p>
  
  <p>Trying to do this with a <code>GridLayout</code> resulted in this:
  <img src="https://i.stack.imgur.com/b4kj8.jpg" alt="enter image description here"></p>
  
  <p>So... what <strong>IS</strong> the easiest way to get the layout I'm looking for (second image)?</p>
  
  <p><code>GroupLayout</code> example code below:</p>
  
  <pre><code>    JFrame frame = new JFrame();
      JPanel panel = new JPanel(new BorderLayout());
      frame.setContentPane(panel);
  
      JPanel longText = new JPanel();
      JPanel shortText = new JPanel();
      JPanel mediumText = new JPanel();
  
      longText.add(new TextField(5));
      longText.add(new JLabel("+"));
      longText.add(new TextField(5));
      shortText.add(new TextField(5));
      shortText.add(new JLabel("+"));
      shortText.add(new TextField(5));
      mediumText.add(new TextField(5));
      mediumText.add(new JLabel("+"));
      mediumText.add(new TextField(5));
  
      JLabel lExample = new JLabel("Long text example:");
      JLabel sExample = new JLabel("Short:");
      JLabel mExample = new JLabel("Medium Example:");
  
      JPanel subPanel = new JPanel();
      GroupLayout layout = new GroupLayout(subPanel);
      subPanel.setLayout(layout);
      layout.setHorizontalGroup(layout.createSequentialGroup()
          .addGroup(layout.createParallelGroup(Alignment.CENTER)
              .addGroup(layout.createSequentialGroup()
                  .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                      .addComponent(lExample)
                      .addComponent(sExample)
                      .addComponent(mExample))
                  .addGroup(layout.createParallelGroup(Alignment.TRAILING)
                      .addComponent(longText)
                      .addComponent(shortText)
                      .addComponent(mediumText))))
              .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER))
      );
      layout.setVerticalGroup(layout.createSequentialGroup()
          .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
              .addComponent(lExample)
              .addComponent(longText))
          .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
              .addComponent(sExample)
              .addComponent(shortText))
          .addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
              .addComponent(mExample).addComponent(mediumText))
      );
  
      JTextArea textArea = new JTextArea() {
          @Override
          public Dimension getPreferredSize() {
              return new Dimension(600,300);
          }
      };
      textArea.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
      textArea.setLineWrap(true);
      textArea.setWrapStyleWord(true);
      textArea.setAutoscrolls(true);
  
      panel.add(subPanel,BorderLayout.NORTH);
      panel.add(textArea,BorderLayout.CENTER);
  
      frame.pack();
      frame.setVisible(true);
  </code></pre>