Name: bkR10101 Date: 10/16/2001
Specification for public Dimension preferredLayoutSize(Container parent):
"Determines the preferred size of the container argument using this grid
layout. The preferred width of a grid layout is the largest preferred
width of any of the widths in the container times the number of columns,
plus the horizontal padding times the number of columns plus one, plus
the left and right insets of the target container. The preferred height
of a grid layout is the largest preferred height of any of the heights
in the container times the number of rows, plus the vertical padding
times the number of rows plus one, plus the top and bottom insets of the
target container".
Sample code:
====================== preferredLS.java ===============================
import java.awt.*;
public class preferredLS {
public static void main(String argv[]) {
int rows = 1; int cols = 2;
int hgap = 2; int vgap = 3;
GridLayout gl = new GridLayout(rows, cols, hgap, vgap);
Container parent = new Container();
parent.setLayout(gl);
parent.setSize(100, 200);
Insets insets = parent.getInsets();
System.out.println("Insets left " + insets.left + " right " +
insets.right + " top " + insets.top +
" bottom " + insets.bottom);
Component comp = new Component(){};
comp.setSize(13, 24);
parent.add(comp, "comp");
System.out.println("comp preferred size " +
comp.getPreferredSize());
Component comp2 = new Component(){};
comp2.setSize(19, 11);
parent.add(comp2, "comp2");
System.out.println("comp2 preferred size " +
comp2.getPreferredSize());
Dimension dim = gl.preferredLayoutSize(parent);
System.out.println("preferred layout size " + dim);
}
}
==================== end of preferredLS.java ===============================
Output under JDK version "1.4.0-beta3-b81" ===============================
~/test
java preferredLS
Insets left 0 right 0 top 0 bottom 0
comp preferred size java.awt.Dimension[width=13,height=24]
comp2 preferred size java.awt.Dimension[width=19,height=11]
preferred layout size java.awt.Dimension[width=40,height=24]
~/test
==========================================================================
So first was created GridLayout with 1 row and 2 columns. Its hgap was
set to 2 and vgap was set to 3. Then created parent container. Then two
components were added to container. Sizes of components are (13, 24) and
(19, 11).
Preferrde layout size returned by gl.preferredLayoutSize(parent):
[width=40,height=24], it is not equal one according specification.
Preferred layout size according specification is
width : max(13, 19) * 2 + 2 * (2+1) + 0 + 0 = 38 + 6 = 44
height : max(24, 11) * 1 + 3 * (1+1) + 0 + 0 = 24 + 6 = 30
======================================================================
|