I am using the following example from the oracle docs:
JComponent panel = ...;
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
// Turn on automatically adding gaps between components
layout.setAutoCreateGaps(true);
// Turn on automatically creating gaps between components that touch
// the edge of the container and the container.
layout.setAutoCreateContainerGaps(true);
// Create a sequential group for the horizontal axis.
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
// The sequential group in turn contains two parallel groups.
// One parallel group contains the labels, the other the text fields.
// Putting the labels in a parallel group along the horizontal axis
// positions them at the same x location.
//
// Variable indentation is used to reinforce the level of grouping.
hGroup.addGroup(layout.createParallelGroup().
addComponent(label1).addComponent(label2));
hGroup.addGroup(layout.createParallelGroup().
addComponent(tf1).addComponent(tf2));
layout.setHorizontalGroup(hGroup);
// Create a sequential group for the vertical axis.
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
// The sequential group contains two parallel groups that align
// the contents along the baseline. The first parallel group contains
// the first label and text field, and the second parallel group contains
// the second label and text field. By using a sequential group
// the labels and text fields are positioned vertically after one another.
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(label1).addComponent(tf1));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(label2).addComponent(tf2));
layout.setVerticalGroup(vGroup);
Here is a link to the site: http://docs.oracle.com/javase/8/docs/api/javax/swing/GroupLayout.html
The example is near the top. This is what is supposed to happen:
"This" being that there is a label with an associate JComponent per row. I would like to extend this to 3, 4, 5... rows. I try to add more rows by adding the following lines:
hGroup.addGroup(layout.createParallelGroup().
addComponent(label3).addComponent(label4));
hGroup.addGroup(layout.createParallelGroup().
addComponent(tf3).addComponent(tf4));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(label3).addComponent(tf3));
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).
addComponent(label4).addComponent(tf4));
This is what I'm getting however:
I cut it off on the left side but as you can see there are 4 textfields and 4 other components. The textfields should ALWAYS be aligned underneath eachother, as well as the components.
Is there something with GroupLayout
or SequentialGroup
that I'm missing, because it seemed very trivial to just add more components the way I did.