0

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:

enter image description here

"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:

enter image description here

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Michael
  • 3,093
  • 7
  • 39
  • 83
  • 1
    For better help sooner post a valid [mcve] – Frakcool Sep 22 '17 at 20:20
  • 1
    `it seemed very trivial` - using GroupLayout is never trivial. Usually only IDE's use it. We can generally create logic groupings of components using different layout managers to achieve a flexible layout. In this case I would suggest a `GridBagLayout` would be easier to create a simple 2 x 2 grid. – camickr Sep 22 '17 at 20:44
  • Please **read** the helpful tag pop-ups before adding them to a question. If you had you might have noticed that the [tag:oracle] tag has nothing to do with this problem. – Andrew Thompson Sep 22 '17 at 21:18
  • 1
    Possible duplicate of [*Can Components be added to groups in* `GroupLayout` *dynamically?*](https://stackoverflow.com/q/41924962/230513) – trashgod Sep 23 '17 at 00:07

0 Answers0