Let's assume that a container has been created that uses a GridBagLayout to manage the size and position
of its child components, and that a width of 400 pixels is needed to display the components using their
minimum sizes. However, let's also assume that when the layout manager prepares to arrange the
components, it determines that the container is 600 pixels wide. In this case, the GridBagLayout must
determine how to distribute the extra 200 pixels to its columns.
Calculating Column Weights
The first step that the GridBagLayout must take is to calculate a weight for each column, and that weight
will determine how much of the extra 200 pixels will be distributed to the column. In the simplest case where
each component has a gridwidth value of 1 (in other words, no component spans multiple columns), the
weight of a column is defined as the largest weightx value of any component within that column. For
example, suppose that the following table represents the weightx values of components in a container:
Since the weight of a column is defined as the maximum weightx value in that column, the weights of
the three columns in this grid are 50, 50, and 25, respectively. As we'll see shortly, neither the
weights' absolute values nor their sum is particularly important, but you may find it easier to work
with round numbers.
In the case where a component spans multiple columns, the calculation of a column's weight value is slightly
more complex. Using a different set of components in some other container, let's suppose that there are three
rows of components in the grid, and that the second row contains a component that spans the second and
third columns as shown below. It's easy to guess the weight of the first column, since it's simply the maximum
weightx value found in that column (1.0). However, it's probably not as obvious how the weight values of
the remaining two columns are calculated:

To understand how the weight values were derived for the second and third columns, it's important to know
that when GridBagLayout calculates column weights, it processes components in order based on their
gridwidth values. In other words, GridBagLayout first examines the weightx values of all components
that have a gridwidth value of 1, then those that have a value of 2, and so on. In this case, the layout
manager's first iteration will process 7 of the 8 components in the container, initially ignoring the component
on the second row that has a gridwidth of 2. In the process of doing so, it calculates a preliminary column
weight of 0.25 for the second column and 0.5 for the third column.
On the GridBagLayout's next iteration, it processes the weightx of the component that spans the
second and third columns, and must distribute that value (3.0) across the two columns. It does this by
distributing the amount proportionally based upon the preliminary weight values of the columns.
Specifically, it adds together the preliminary column weight values and divides the weight value of each
column by that sum to determine a percentage of the spanning component's weightx value that should
be distributed to the column.
For example, in this case, the preliminary weight values of the second and third columns are 0.25 and 0.5,
respectively, and the sum of these two values is 0.75. Dividing the preliminary weight of the second column
by 0.75 produces a value of 0.33, and dividing the third column's preliminary weight by the same 0.75
produces a value of 0.67. These values represent the percentage of the spanning component's weightx value
that will be distributed to each column. Specifically, one-third (33%) will be assigned to the second column,
and the remaining two-thirds (67%) will be assigned to the third column. Since the weight of the component
that spans the two columns is 3, it represents a weight of 1 (3.0 * 0.33 = 1.0) for the second column and 2 (3.0
* 0.67 = 2.0) for the third.
Since the component in the second row represents a weightx value of 1 for the second column and 2 for the
third column, the second column's final weight value is 1 and the third column's final weight is 2.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|