Re: GridBagBlues #2376234
- From: Ian Wilson <scobloke2@xxxxxxxxxxxxx>
- Date: Thu, 29 Jun 2006 11:54:13 +0100
A. Bolmarcich wrote:
On 2006-06-28, Ian Wilson <scobloke2@xxxxxxxxxxxxx> wrote:
In the GridBagLayout example that follows, all the panels nest nicely within the frame with no spare space.
Now change the last three lines of the constructor
from
add(panel5, new GBC(3, 1, 1, GBC.REMAINDER));
//add(panel5, new GBC(3, 1, 1, GBC.RELATIVE));
//add(panel6, new GBC(3, 3, 1, 1));
to
//add(panel5, new GBC(3, 1, 1, GBC.REMAINDER));
add(panel5, new GBC(3, 1, 1, GBC.RELATIVE));
add(panel6, new GBC(3, 3, 1, 1));
Now there is a large blank area at the bottom of the frame. Can anyone tell me why?
[example program snipped]
The blank area at the bottom of the frame is there due to the details
of how GridBagLayout determines the preferred height of each row.
GridBagLayout loops over the components in the order that they are in
the container and checks if the preferred height of the component fits
in the preferred row heights determined by the previous iterations of
the loop. If it does not, GridBagLayout increases the preferred
heights of the rows that the component is in.
That approach produces generally expected row heights as long as each
iteration of the loop increases the height of at most one row. That
approach can produce unexpected row heights when an iteration of the
loop distributes increases in height over multiple rows, especially
when the weighty constraint of all the rows is 0.
With your example, the blank area will not be there if you add the
components in the order panel0, panel3, panel6, panel5, panel4, panel1,
panel2. With this order
height of row 0 = height of panel0
height of row 1 = height of panel3
height of row 4 = height of panel6
height of row 2 = height of panel5 - height of row 1
height of row 3 = height of panel4 - height of row 2
Thanks, I tried that and it works. I struggled a bit to understand how you arrived at that ordering, hence ....
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.
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.
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.
.
- Follow-Ups:
- Re: GridBagBlues #2376234
- From: A. Bolmarcich
- Re: GridBagBlues #2376234
- References:
- GridBagBlues #2376234
- From: Ian Wilson
- Re: GridBagBlues #2376234
- From: A. Bolmarcich
- GridBagBlues #2376234
- Prev by Date: AbstractAction
- Next by Date: Re: Button event
- Previous by thread: Re: GridBagBlues #2376234
- Next by thread: Re: GridBagBlues #2376234
- Index(es):