Re: GridBagBlues #2376234



On 2006-06-29, Ian Wilson <scobloke2@xxxxxxxxxxxxx> wrote:
Thanks, I tried that and it works. I struggled a bit to understand how
you arrived at that ordering, hence ....

The key part of the ordering in your example is to have panel5 in the
container before panel4. What GridBagLayout actually does in more
complicated than I want to try to explain here. Also, I incorrectly
described the height of panel6 affecting the height of row 4; it
affects the height of row 3.

Rule of thumb 1:
If the layout produces excess row height (empty space at bottom of
container), order the add()s by GridBagConstraint.height,
GridBagConstraint.gridy ascending.

Something similar is true about column widths.

Spurred by the provided solution, I did some more experimentation and
was also able to obtain the desired compact layout whilst keeping my
'natural' ordering. I replaced my use of GridBagConstraint.RELATIVE and
GridBagContraint.REMAINDER with actual heights (e.g. 2 or 3) and I set
the height of panel4 to 1, since it didn't actually need to flow into
lower rows.

With the "natural" ordering and panel4 having a gridheight of 1, the
height of panel4 affects the height of row 2, the only row it is in.
When panel4 has a gridheight of 2, its height affects the height of row
3, the last row that it occupies, which causes row 3 to have a height
greater than that of panel6.

Rule of thumb 2:
If the layout produces excess space in the container and you have a
component that has empty space beyond it, but which does not need to use
that empty space, don't set that component's GridBagConstraint.width or
height to more than it needs.

I suspect this rule is less likely to succeed if some of the components
can grow when the container is resized.

Actually, if the weights of the columns (or rows) is not zero,
GridBagLayout is more likely to produce expected column widths (or row
heights). However, the way that GridBagLayout determines the column and
row weights from the weightx and weighty value of the components, may not
produce the expected results.
.


Quantcast