Java Swing How to - Use GroupLayout to layout labels and JTextFields








Question

We would like to know how to use GroupLayout to layout labels and JTextFields.

Answer

import java.awt.Dimension;
//  w  ww .  j  av a 2s  .co m
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Main {
  static int NUM = 20;
  JTextField[] fields = new JTextField[NUM];
  JLabel[] labels = new JLabel[NUM];

  private JPanel create() {
    JPanel panel = new JPanel();
    GroupLayout layout = new GroupLayout(panel);
    panel.setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);
    GroupLayout.ParallelGroup parallel = layout.createParallelGroup();
    layout.setHorizontalGroup(layout.createSequentialGroup().addGroup(parallel));
    GroupLayout.SequentialGroup sequential = layout.createSequentialGroup();
    layout.setVerticalGroup(sequential);
    for (int i = 0; i < NUM; i++) {
      labels[i] = new JLabel(String.valueOf(i + 1), JLabel.RIGHT);
      fields[i] = new JTextField(String.valueOf("" + (i + 1)));
      labels[i].setLabelFor(fields[i]);
      parallel.addGroup(layout.createSequentialGroup().addComponent(labels[i])
          .addComponent(fields[i]));
      sequential.addGroup(layout
          .createParallelGroup(GroupLayout.Alignment.BASELINE)
          .addComponent(labels[i]).addComponent(fields[i]));
      layout.linkSize(SwingConstants.HORIZONTAL, labels[i], labels[0]);
    }
    return panel;
  }

  public static void main(String[] args) {
    JPanel panel = new Main().create();
    JScrollPane jsp = new JScrollPane(panel) {
      @Override
      public Dimension getPreferredSize() {
        return new Dimension(320, 240);
      }
    };
    JOptionPane.showMessageDialog(null, jsp, "Data", JOptionPane.PLAIN_MESSAGE);

  }
}