Re: GridBagBlues #2376234



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
.


Quantcast