0

I've built a JFrame with two panels aligned like this:

panel1

panel2

So their width matches. But panel2's width is greater than panel1's would be. Therefore the panel is stretched but not the components inside. I've built up panel1's layout with a group layout like this:

label1 label2

label3 label4

               button1 #### <-- there is still space but the button does not align with its parent's right

Here's my code:

GroupLayout layout = new GroupLayout(this);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createParallelGroup()
        .addGroup(layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                        .addComponent(label1)
                        .addComponent(label2))
                .addGroup(layout.createSequentialGroup()
                        .addComponent(label3)
                        .addComponent(label4)))
        .addComponent(button1, GroupLayout.Alignment.TRAILING));

layout.setVerticalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup()
                .addComponent(label1)
                .addComponent(label2))
        .addGroup(layout.createParallelGroup()
                .addComponent(label3)
                .addComponent(label4))
        .addGroup(layout.createParallelGroup()
                .addComponent(button1)));

setLayout(layout);

Is there any way to use all the space available? Shouldn't this be the default behavior?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
torkleyy
  • 1,137
  • 9
  • 26

0 Answers0