Combining Layout Mgrs (Box, Border)

Sorry if this is an obvious/easy question - I checked API, Tut, Google, etc.

We have a JPanel that uses BoxLayout (Y align). It adds a JLabel as a

header, then a toolbar, then two groups of JPanels (only 1 is meant to be

shown at a time depending on what data is avalable). One group of JPanels

is shown near the top while the other one looks centered (although it's more

like it's bottom aligned with some extra padding making it appear centered).

Anyway, I tried a whole bunch of things to get both panel groups top aligned

but what finally worked was removing the toolbar. After that, I also found that

I could still have the toolbar as long as I removed the specific BorderLayout it

was using. So...

JPanel toolbarPanel = new JPanel(new BorderLayout());

toolbarPanel.add(< a JToolBar with some buttons >, BorderLayout.PAGE_START);

mainPanel.add(toolbarPanel);

is now...

JPanel toolbarPanel = new JPanel();

toolbarPanel.add(< a JToolBar with some buttons >);

mainPanel.add(toolbarPanel);

Can someone explain, or point me to some info that goes into

detail on what happens when combining different layout mgrs

(and/or how different components affect alignment)? Thanks!

[1306 byte] By [ShaneDavisa] at [2007-11-27 2:23:46]
# 1

[url http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html]How to Use Box Layout[/url]

Don't know exactly whats happening, but you need to understand how each layout manager uses the information from the preferred/minimum/maximum size of each component.

For example FlowLayout just used the preferred size and then places components in a line one following the other.

GridLayout on the other hand looks for the largest preferred size of all components and then resizes every component to be equal to the largest size.

Box Layout is one the the few layout managers that actually looks at the minumum and maximum sizes as well as the preferred size. Different components use different values for the min and max sizes. Some simply use the preferred size wherease other would use 0 and Integer.MAX.

Box Layout also take into consideration the alignmentX and alignmentY properties of each component.

So read the tutorial and see how each of those values is used by BoxLayout to determine size and location of each component.

camickra at 2007-7-12 2:29:48 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for the reply. That's pretty much what I did and I still don't understand

what's going on but, as you said, it's probably in there somewhere. I'll just have

to go through all of it more carefully.

I was really expecting it to be something related to the panel group itself rather

than any of the other components but then taking it out and just adding a simple

JButton caused the button to be at the bottom of the panel too (and no alignment

calls would change that). The JToolbar seemed standard but without it (or without

it using BorderLayout), the components after it were at the top.

ShaneDavisa at 2007-7-12 2:29:48 > top of Java-index,Desktop,Core GUI APIs...