I'm very new to Java Swing. I'm using y-axis BoxLayout on an "outer" panel that contains 2 inner panels. Here you can see with this basic code, the 2 inner panels get an equal share of the height avaiable in the box layout (first inner panel yellow, second innner panel orange)
// main test frame
public class MainFrame extends JFrame {
public MainFrame()
{
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(1,1));
// outer panel: groupWrapPanel with BoxLayout
JPanel groupWrapPanel = new JPanel();
groupWrapPanel.setBackground(Color.blue);
groupWrapPanel.setLayout(new BoxLayout(groupWrapPanel, BoxLayout.Y_AXIS));
// inner panel 1: headerPanel (yellow)
JPanel headerPanel = new JPanel();
headerPanel.setBackground(Color.yellow);
groupWrapPanel.add(headerPanel);
// inner panel 2: headerNotesWrap (orange)
JPanel headerNotesWrap = new JPanel();
headerNotesWrap.setBackground(Color.orange);
groupWrapPanel.add(headerNotesWrap);
this.add(groupWrapPanel);
this.setVisible(true);
}
}
But then when I add seperate layout manager to one of the inner panels, the layout of the outer panel changes, and the 2 inner panels no longer get an equal share of the height of the outer panel. I can do this by adding a single line of code:
headerPanel.setLayout(new BorderLayout());
Can someone please explain why the layout of the outer panel changes when I specify a layout manager for the inner panel, and how I can prevent this.
Thank you.
EDIT
This is a related question and is in response to a comment someone made.
"A BoxLayout will determine the preferred size of each component. If there is extra space available it will then allocate the space equally up to the maximum size of each component. That would explain why each panel is 50/50 in the first case."
If that's the case, how come if I change the 2 inner components from panels to labels, they no longer stretch to fill the available space inside out the outer panel? like so:
public class MainFrame extends JFrame {
public MainFrame()
{
// we're going to create what shall be known as a "Group Box",
// which groups things together
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(1,1));
// outer panel: groupWrapPanel with BoxLayout
JPanel groupWrapPanel = new JPanel();
groupWrapPanel.setBackground(Color.blue);
groupWrapPanel.setLayout(new BoxLayout(groupWrapPanel, BoxLayout.Y_AXIS));
// inner label 1
JLabel label1 = new JLabel("Label 1");
label1.setBackground(Color.yellow);
label1.setOpaque(true);
groupWrapPanel.add(label1);
// inner label 2
JLabel label2 = new JLabel("Label 2");
label2.setBackground(Color.orange);
label2.setOpaque(true);
groupWrapPanel.add(label2);
this.add(groupWrapPanel);
this.setVisible(true);
}
}
Thanks again