Java Reference
In-Depth Information
There are three constructors for creating the GridLayout layout manager:
public GridLayout()
public GridLayout(int rows, int columns)
public GridLayout(int rows, int columns, int hgap, int vgap)
Typically, you would explicitly specify the overall grid size for your GridLayout -managed
container. However, you can set the number of rows or columns to be zero, and the layout will
grow without bounds in the direction with a zero setting.
Caution If both rows and columns are specified to be zero to the GridLayout constructor, a runtime
exception of IllegalArgumentException will be thrown.
The actual number of rows and columns drawn is based on the number of components
within the container. The GridLayout tries to observe the number of rows requested first.
If the requested number of rows is nonzero, the number of columns is determined by
( # of components + rows - 1)/ rows . If your request is for zero rows, the number of rows to use is
determined by a similar formula: ( # of components + columns - 1)/ columns . Table 10-1 demon-
strates this calculation. The last entry in the table is of special interest: if you request a 3×3 grid
but place only four components in the layout, you actually get a 2×2 layout as a result. If you do
not want to be surprised, size the GridLayout based on the actual number of objects you plan
to add to the display.
Table 10-1. GridLayout Row/Column Calculation
Rows
Columns
# Components
Display Rows
Display Columns
0
1
10
10
1
0
2
10
5
2
1
0
10
1
10
2
0
10
2
5
2
3
10
2
5
2
3
20
2
10
3
2
10
3
4
3
3
3
3
1
3
3
4
2
2
GridBagLayout Class
GridBagLayout is the most complex and most flexible of the layout managers. Although it sounds
like it should be a subclass of GridLayout , it's a different beast altogether. With GridLayout ,
elements are arranged in a rectangular grid, and each element in the container is sized identically
 
Search WWH ::




Custom Search