Java Reference
In-Depth Information
The main job of the TableLayout is done in the layoutContainer() method. The method
resizes each component that is added to the container according to its height and width and the
container size. If there is remaining space, all components are scaled with an equal factor, calculated by
dividing the space available by the minimum layout size:
public void layoutContainer (Container container) {
int count = container.getComponentCount();
int rows = getRowCount (container);
if (count == 0) return;
Insets insets = container.getInsets();
Dimension size = container.getSize();
int x0 = insets.left;
int y0 = insets.top;
int w0 = size.width - x0 - insets.right;
int h0 = size.height - y0 - insets.bottom;
int [] mcw = new int [cols];
int [] mrh = new int [rows];
Dimension min = getMinimumSizes (container, mcw, mrh);
// calculate a scale factor
int scx = ((w0-cols+1) << 8) / min.width;
int scy = ((h0-rows+1) << 8) / min.height;
int i = 0;
for (int y = 0; y < rows; y++) {
int x1 = x0;
int h = (mrh [y] * scy) >> 8;
for (int x = 0; x < cols && i < count; x++) {
int w = (mcw [x] * scx) >> 8;
container.getComponent (i++).setBounds (x1, y0, w, h);
x1 += w + 1;
}
y0 += h + 1;
}
}
Because we don't need additional layout constraints such as NORTH or CENTER for the
BorderLayout , we don't need to keep track of adding and removing components. Thus, the
implementations of addLayoutComponent() and removeLayoutComponent() are left
empty:
public void addLayoutComponent (String where, Component component) {
}
public void removeLayoutComponent (Component component) {
}
Note that if layout constraints are important, in most cases it is more appropriate to implement the
improved LayoutManager2 class, which can handle arbitrary objects as layout constraints instead of
String s.
Search WWH ::




Custom Search