Java Reference
In-Depth Information
Example 10•9: ColumnLayout.java (continued)
}
// Set the size and position of this kid
kid.setBounds(x, y, pref.width, pref.height);
y += pref.height + spacing;
// Get Y position of the next one
}
}
/** The Container calls this to find out how big the layout should to be */
public Dimension preferredLayoutSize(Container parent) {
return layoutSize(parent, 1);
}
/** The Container calls this to find out how big the layout must be */
public Dimension minimumLayoutSize(Container parent) {
return layoutSize(parent, 2);
}
/** The Container calls this to find out how big the layout can be */
public Dimension maximumLayoutSize(Container parent) {
return layoutSize(parent, 3);
}
// Compute min, max, or preferred size of all the visible children
protected Dimension layoutSize(Container parent, int sizetype) {
int nkids = parent.getComponentCount();
Dimension size = new Dimension(0,0);
Insets insets = parent.getInsets();
int num_visible_kids = 0;
// Compute maximum width and total height of all visible kids
for(int i = 0; i < nkids; i++) {
Component kid = parent.getComponent(i);
Dimension d;
if (!kid.isVisible()) continue;
num_visible_kids++;
if (sizetype == 1) d = kid.getPreferredSize();
else if (sizetype == 2) d = kid.getMinimumSize();
else d = kid.getMaximumSize();
if (d.width > size.width) size.width = d.width;
size.height += d.height;
}
// Now add in margins and stuff
size.width += insets.left + insets.right + 2*margin_width;
size.height += insets.top + insets.bottom + 2*margin_height;
if (num_visible_kids > 1)
size.height += (num_visible_kids - 1) * spacing;
return size;
}
// Other LayoutManager(2) methods that are unused by this class
public void addLayoutComponent(String constraint, Component comp) {}
public void addLayoutComponent(Component comp, Object constraint) {}
public void removeLayoutComponent(Component comp) {}
public void invalidateLayout(Container parent) {}
public float getLayoutAlignmentX(Container parent) { return 0.5f; }
public float getLayoutAlignmentY(Container parent) { return 0.5f; }
}
Search WWH ::




Custom Search