Final Theory
Some final details to consider.
Dynamic Rows and Columns
As in a spreadsheet, rows and columns can be added at any time. Components are moved down and to the
right as necessary. Rows and columns can also be removed or resized at runtime. The TableLayout class has
methods to accomidate this.
Preferred Layout Size
One of the responsibilities of a layout manager is to determine the preferred size of a container based on the
container's components, the component's constraints, and the layout manager's configuration. The preferred size
of a container is typically the size that will allow all components to be at least as large as their preferred
size. When frame.pack is called, the frame's layout manager is asked to determine the frame's
preferred size, and that size is given to the frame.
TableLayout uses a complex algorithm to determine the preferred layout size. The entire algorithm is beyond
the scope of this article, but the fundamental idea behind the algorithm is to add the preferred sizes of all
rows and columns to arrive at the container's preferred size. The preferred size of a column is fixed if the
column is given an absolute size. For scalable, fill, and preferred columns the preferred width is determine
by the column's percentage or fill/prefer attribute and the preferred widths of all components contained either
partly or wholly in the column. Since a component can ocuppy many scalable columns and a single column can
contain many such component, the preferred size can be tricky to determine.
However, the final behavior is simple. Any component in an absolute column will be given an absolute width.
Any component partly or wholly in a scalable, fill, or preferred column will be given a width equal or
greater than its preferred width. The component will be given a greater width only if necessary to ensure
that another component is given its preferred width.
Layout managers also must be able to compute the minimum size of a container, which is typically the size
the container must be to ensure all of its components are at least the size defined by their
getMinimumSize method. The minimum size of a container is determined almost the same way
that the preferred size is determined.
Onward
Now that we've covered the theory behind TableLayout, let's move on to some examples.
|