Title: Professional Java Programming
ISBN: 186100382x
US Price: $ 59.99
Canadian Price:
C$ 89.95
UK Price: £ 45.99
© Wrox Press Limited, US and UK.

Converting Weights to Percentages

Now that a weight value has been assigned to each column, those values can be used to determine the amount of extra space that should be allocated to each column. This is done by first calculating the sum of all column weight values and dividing each column's weight by that sum. In this case, the sum of all the weights is 4 (1.0 + 1.0 + 2.0 = 4), and the first column is given one-fourth (25%) of the extra space. Similarly, the second column is allocated one-fourth (25%) of the space, and the third and final column receives the remaining two-fourths (50%). For the first example (with column weights fo 50, 50, and 25, with a sum of 125), the first two columns receive two-fifths of the extra space each (50 / 125 = 0.4 or 40%), while the last column receives the remaining one-fifth (20%).

Distributing the Extra Space

Having calculated the percentage of extra space that should be added to the width of each column, it's easy to determine the number of pixels that will be distributed in our example. Since there are 200 extra pixels, the first and second columns will be made wider by 50 pixels (200 * 0.25 = 50), while the third column becomes 100 pixels wider (200 * 0.5 = 100).

Although this example describes a situation where extra space was being added to columns, the same principles apply when space needs to be taken away. For example, if the container had been 200 pixels smaller than it needed to be instead of 200 larger, the three columns would have been reduced in size by 50, 50, and 100 pixels, respectively.

General Guidelines for Setting Weights

As you can see, GridBagLayout's behavior with respect to weight values is somewhat complex. However, you can reduce the complexity in some cases by assigning weightx values only to the components in a single row and weighty values to those in a particular column. If you do so, you're effectively setting the weight value for the entire row or column when you specify it for the component, which makes it easier to predict how space will be added or taken away.

In addition, you may find it easier to use weight values that add up to some round number such as 1.0 or 100.0, allowing you to easily associate a weight value with a percentage. For example, given the previous grid, you could specify the weightx values only on the components in the first row as shown below:

In this case, only the components in the first row were assigned weightx values and the sum of those values is 100, making it much more obvious how space will be added or removed from the columns. Specifically, the first and second columns are allocated 25% of any extra space, while the third one is given the remaining 50%. You may have noticed that in some examples, relatively large weight values (for example, 50, 10, 15, etc.) were used, while smaller ones were specified at other times. This was done deliberately to illustrate a point: the absolute size of weight values used is unimportant. What matters is how large those values are relative to one another. In other words, you can produce the same results using fractional values as you can by using very large numbers. For example, three columns with weights of 0.25, 0.25, and 0.50 have space distributed to them in exactly the same amounts that they would if the columns had weights of 100, 100 and 200.

It's also important to remember that weights do not necessarily represent the relative sizes of the cells, but rather the relative amount of space that will be added to or taken away from those cells. For example, if you create a grid with two columns and the second column is assigned a weight that's twice as large as the first, you should not expect the second column to be twice as large. However, you can correctly assume that the second column will be given twice as much extra space as the first if excess space is distributed to them.

How to Add Java Applets to Your Site

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.