Tuesday, March 10, 2009

Refresh on GridBagLayout

I have to work on a GUI in Java recently. Since it works with JDK1.5 so that the most convinient and flexiable GroupLayout can't be a candidate. The NetBean 6.5 does not provide automatic conversion from GroupLayout to GridBagLayout as what has been provided in JBuilder3.x back to more than 10 years in 1998, and the visual Editor for eclipse does not fully support free GUI design, so that I have to code it by hand. Once again it reminds me that Borland's tools are the best.

This is a good chance for me to refresh the knowledge on layout managers in Java. Most of the content below is from http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html.

GridBagLayout is the second most flexiable and powerful layout manager next to GroupLayout. It places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. Not all rows necessarily have the same height. Similarly, not all columns necessarily have the same width. Essentially, GridBagLayout places components in rectangles (cells) in a grid, and then uses the components' preferred sizes to determine how big the cells should be.

This resizing behavior is based on weights the program assigns to individual components in the GridBagLayout. The way the program specifies the size and position characteristics of its components is by specifying constraints for each component. The preferred approach to set constraints on a component is to use the Container.add variant, passing it a GridBagConstraints object.

It is possible to reuse the same GridBagConstraints instance for multiple components, even if the components have different constraints. However, it is recommended that you do not reuse GridBagConstraints, as this can very easily lead to you introducing subtle bugs if you forget to reset the fields for each new instance

Other attributes are easy to understand, below are the ones need to pay more attention to.

weightx, weighty

Specifying weights is an art that can have a significant impact on the appearance of the components a GridBagLayout controls. Weights are used to determine how to distribute space among columns (weightx) and among rows (weighty); this is important for specifying resizing behavior.

Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should get more space. For each column, the weight is related to the highest weightx specified for a component within that column, with each multicolumn component's weight being split somehow between the columns the component is in. Similarly, each row's weight is related to the highest weighty specified for a component within that row. Extra space tends to go toward the rightmost column and bottom row.

No comments: