Reputation: 1122
I'm trying to achieve a design whereby one of my JPanel's heights is only set to the height of it's inner components. Currently it's laid out like so:
JPanel (BoxLayout)
JPanel (CardLayout)
JPanel (BoxLayout)
JPanel (BoxLayout)
JPanel (FlowLayout)
It currently displays as so, I want the top bit to expand the both on the x and y axis pushing the "Your Name" to above the status bar.
I need to use BoxLayouts it seems so that it flows from top to bottom.
Please help!
Upvotes: 1
Views: 200
Reputation: 324118
I want the top bit to expand the both on the x and y axis pushing the "Your Name" to above the status bar.
Then is sounds like you can use a BorderLayout as the top level panel, instead of the BoxLayout.
Since the default layout manager for the content pane of the frame is a BorderLayout you can just add the panels directly to the content pane of the frame:
frame.add(cardLayoutPanel, BorderLayout.CENTER);
frame.add(flowLayoutPanel, BorderLayout.PAGE_END);
When you add a panel to the "PAGE_END", its preferred height is respected.
When you add a panel to the "CENTER" it gets all the extra spaces available in the frame.
Read the Swing tutorial on Layout Managers for more information and working examples.
I need to use BoxLayouts it seems so that it flows from top to bottom.
You don't need to use a BoxLayout (as demonstrated above), but you can still use it if you want. BoxLayout respects the maximum size of a panel. So you could override the getPreferredSize()
method of your panel using the flow layout to return the preferred height of the panel.
@Override
public Dimension getMaximumSize()
{
Dimension preferred = super.getPreferredSize();
Dimension maximum = super.getMaximumSize();
maximum.height = preferred.height;
return maximum;
}
Now the height of the flow panel will be respected and all the extra space will go to the other panel.
Upvotes: 1