Java Reference
In-Depth Information
Before we can implement the methods for calculating the minimal or preferred layout size or
performing the actual layout itself, it makes sense to think about a helper method caring about the
common calculations of all those methods.
For both calculating the minimum layout sizes and performing the actual layout task, it is necessary to
know the minimum sizes of the rows and columns. The height of a row is determined by the maximum
cell height of that row. The width of a column is determined by the maximum cell width of that column.
The helper method getMinimumSizes() fills the given int arrays with the corresponding
calculations and returns the sums in a Dimension object:
public Dimension getMinimumSizes (Container container,
int [] mcw, int [] mrh) {
int count = container.getComponentCount();
Dimension sum = new Dimension (0, 0);
int i = 0;
for (int y = 0; y < mrh.length; y++) {
for (int x = 0; x < cols && i < count; x ++) {
Dimension ms = container.getComponent
(i++).getMinimumSize();
mcw [x] = Math.max (mcw [x], ms.width);
mrh [y] = Math.max (mrh [y], ms.height);
}
sum.height += mrh [y];
}
for (int x = 0; x < cols; x++)
sum.width += mcw [x];
return sum;
}
Using this getMinimumSizes() method, the minimumLayoutSize() method of the
LayoutManager interface can be implemented easily by just adding the container insets to the
returned dimensions:
public Dimension minimumLayoutSize (Container container) {
Insets insets = container.getInsets();
int rows = getRowCount (container);
Dimension result = getMinimumSizes
(container, new int [cols], new int [rows]);
result.width += cols - 1 + insets.left + insets.right;
result.height += rows - 1 + insets.top + insets.bottom;
return result;
}
For the limited screen sizes of PDAs, it seems appropriate to return the minimumLayoutSize also
as a preferred layout size:
public Dimension preferredLayoutSize (Container container) {
return minimumLayoutSize (container);
}
Search WWH ::




Custom Search