KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > GridLayout


1 /*
2  * @(#)GridLayout.java 1.39 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt;
9
10 /**
11  * The <code>GridLayout</code> class is a layout manager that
12  * lays out a container's components in a rectangular grid.
13  * The container is divided into equal-sized rectangles,
14  * and one component is placed in each rectangle.
15  * For example, the following is an applet that lays out six buttons
16  * into three rows and two columns:
17  * <p>
18  * <hr><blockquote>
19  * <pre>
20  * import java.awt.*;
21  * import java.applet.Applet;
22  * public class ButtonGrid extends Applet {
23  * public void init() {
24  * setLayout(new GridLayout(3,2));
25  * add(new Button("1"));
26  * add(new Button("2"));
27  * add(new Button("3"));
28  * add(new Button("4"));
29  * add(new Button("5"));
30  * add(new Button("6"));
31  * }
32  * }
33  * </pre></blockquote><hr>
34  * <p>
35  * If the container's <code>ComponentOrientation</code> property is horizontal
36  * and left-to-right, the above example produces the output shown in Figure 1.
37  * If the container's <code>ComponentOrientation</code> property is horizontal
38  * and right-to-left, the example produces the output shown in Figure 2.
39  * <p>
40  * <center><table COLS=2 WIDTH=600 summary="layout">
41  * <tr ALIGN=CENTER>
42  * <td><img SRC="doc-files/GridLayout-1.gif"
43  * alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 1 then 2.
44  * Row 2 shows buttons 3 then 4. Row 3 shows buttons 5 then 6.">
45  * </td>
46  *
47  * <td ALIGN=CENTER><img SRC="doc-files/GridLayout-2.gif"
48  * alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 2 then 1.
49  * Row 2 shows buttons 4 then 3. Row 3 shows buttons 6 then 5.">
50  * </td>
51  * </tr>
52  *
53  * <tr ALIGN=CENTER>
54  * <td>Figure 1: Horizontal, Left-to-Right</td>
55  *
56  * <td>Figure 2: Horizontal, Right-to-Left</td>
57  * </tr>
58  * </table></center>
59  * <p>
60  * When both the number of rows and the number of columns have
61  * been set to non-zero values, either by a constructor or
62  * by the <tt>setRows</tt> and <tt>setColumns</tt> methods, the number of
63  * columns specified is ignored. Instead, the number of
64  * columns is determined from the specified number or rows
65  * and the total number of components in the layout. So, for
66  * example, if three rows and two columns have been specified
67  * and nine components are added to the layout, they will
68  * be displayed as three rows of three columns. Specifying
69  * the number of columns affects the layout only when the
70  * number of rows is set to zero.
71  *
72  * @version 1.39, 12/19/03
73  * @author Arthur van Hoff
74  * @since JDK1.0
75  */

76 public class GridLayout implements LayoutManager JavaDoc, java.io.Serializable JavaDoc {
77     /**
78      * This is the horizontal gap (in pixels) which specifies the space
79      * between columns. They can be changed at any time.
80      * This should be a non-negative integer.
81      *
82      * @serial
83      * @see #getHgap()
84      * @see #setHgap(int)
85      */

86     int hgap;
87     /**
88      * This is the vertical gap (in pixels) which specifies the space
89      * between rows. They can be changed at any time.
90      * This should be a non negative integer.
91      *
92      * @serial
93      * @see #getVgap()
94      * @see #setVgap(int)
95      */

96     int vgap;
97     /**
98      * This is the number of rows specified for the grid. The number
99      * of rows can be changed at any time.
100      * This should be a non negative integer, where '0' means
101      * 'any number' meaning that the number of Rows in that
102      * dimension depends on the other dimension.
103      *
104      * @serial
105      * @see #getRows()
106      * @see #setRows(int)
107      */

108     int rows;
109     /**
110      * This is the number of columns specified for the grid. The number
111      * of columns can be changed at any time.
112      * This should be a non negative integer, where '0' means
113      * 'any number' meaning that the number of Columns in that
114      * dimension depends on the other dimension.
115      *
116      * @serial
117      * @see #getColumns()
118      * @see #setColumns(int)
119      */

120     int cols;
121
122     /**
123      * Creates a grid layout with a default of one column per component,
124      * in a single row.
125      * @since JDK1.1
126      */

127     public GridLayout() {
128     this(1, 0, 0, 0);
129     }
130
131     /**
132      * Creates a grid layout with the specified number of rows and
133      * columns. All components in the layout are given equal size.
134      * <p>
135      * One, but not both, of <code>rows</code> and <code>cols</code> can
136      * be zero, which means that any number of objects can be placed in a
137      * row or in a column.
138      * @param rows the rows, with the value zero meaning
139      * any number of rows.
140      * @param cols the columns, with the value zero meaning
141      * any number of columns.
142      */

143     public GridLayout(int rows, int cols) {
144     this(rows, cols, 0, 0);
145     }
146
147     /**
148      * Creates a grid layout with the specified number of rows and
149      * columns. All components in the layout are given equal size.
150      * <p>
151      * In addition, the horizontal and vertical gaps are set to the
152      * specified values. Horizontal gaps are placed between each
153      * of the columns. Vertical gaps are placed between each of
154      * the rows.
155      * <p>
156      * One, but not both, of <code>rows</code> and <code>cols</code> can
157      * be zero, which means that any number of objects can be placed in a
158      * row or in a column.
159      * <p>
160      * All <code>GridLayout</code> constructors defer to this one.
161      * @param rows the rows, with the value zero meaning
162      * any number of rows
163      * @param cols the columns, with the value zero meaning
164      * any number of columns
165      * @param hgap the horizontal gap
166      * @param vgap the vertical gap
167      * @exception IllegalArgumentException if the value of both
168      * <code>rows</code> and <code>cols</code> is
169      * set to zero
170      */

171     public GridLayout(int rows, int cols, int hgap, int vgap) {
172     if ((rows == 0) && (cols == 0)) {
173         throw new IllegalArgumentException JavaDoc("rows and cols cannot both be zero");
174     }
175     this.rows = rows;
176     this.cols = cols;
177     this.hgap = hgap;
178     this.vgap = vgap;
179     }
180
181     /**
182      * Gets the number of rows in this layout.
183      * @return the number of rows in this layout
184      * @since JDK1.1
185      */

186     public int getRows() {
187     return rows;
188     }
189
190     /**
191      * Sets the number of rows in this layout to the specified value.
192      * @param rows the number of rows in this layout
193      * @exception IllegalArgumentException if the value of both
194      * <code>rows</code> and <code>cols</code> is set to zero
195      * @since JDK1.1
196      */

197     public void setRows(int rows) {
198     if ((rows == 0) && (this.cols == 0)) {
199         throw new IllegalArgumentException JavaDoc("rows and cols cannot both be zero");
200     }
201     this.rows = rows;
202     }
203
204     /**
205      * Gets the number of columns in this layout.
206      * @return the number of columns in this layout
207      * @since JDK1.1
208      */

209     public int getColumns() {
210     return cols;
211     }
212
213     /**
214      * Sets the number of columns in this layout to the specified value.
215      * Setting the number of columns has no affect on the layout
216      * if the number of rows specified by a constructor or by
217      * the <tt>setRows</tt> method is non-zero. In that case, the number
218      * of columns displayed in the layout is determined by the total
219      * number of components and the number of rows specified.
220      * @param cols the number of columns in this layout
221      * @exception IllegalArgumentException if the value of both
222      * <code>rows</code> and <code>cols</code> is set to zero
223      * @since JDK1.1
224      */

225     public void setColumns(int cols) {
226     if ((cols == 0) && (this.rows == 0)) {
227         throw new IllegalArgumentException JavaDoc("rows and cols cannot both be zero");
228     }
229     this.cols = cols;
230     }
231
232     /**
233      * Gets the horizontal gap between components.
234      * @return the horizontal gap between components
235      * @since JDK1.1
236      */

237     public int getHgap() {
238     return hgap;
239     }
240     
241     /**
242      * Sets the horizontal gap between components to the specified value.
243      * @param hgap the horizontal gap between components
244      * @since JDK1.1
245      */

246     public void setHgap(int hgap) {
247     this.hgap = hgap;
248     }
249     
250     /**
251      * Gets the vertical gap between components.
252      * @return the vertical gap between components
253      * @since JDK1.1
254      */

255     public int getVgap() {
256     return vgap;
257     }
258     
259     /**
260      * Sets the vertical gap between components to the specified value.
261      * @param vgap the vertical gap between components
262      * @since JDK1.1
263      */

264     public void setVgap(int vgap) {
265     this.vgap = vgap;
266     }
267
268     /**
269      * Adds the specified component with the specified name to the layout.
270      * @param name the name of the component
271      * @param comp the component to be added
272      */

273     public void addLayoutComponent(String JavaDoc name, Component JavaDoc comp) {
274     }
275
276     /**
277      * Removes the specified component from the layout.
278      * @param comp the component to be removed
279      */

280     public void removeLayoutComponent(Component JavaDoc comp) {
281     }
282
283     /**
284      * Determines the preferred size of the container argument using
285      * this grid layout.
286      * <p>
287      * The preferred width of a grid layout is the largest preferred
288      * width of all of the components in the container times the number of
289      * columns, plus the horizontal padding times the number of columns
290      * minus one, plus the left and right insets of the target container.
291      * <p>
292      * The preferred height of a grid layout is the largest preferred
293      * height of all of the components in the container times the number of
294      * rows, plus the vertical padding times the number of rows minus one,
295      * plus the top and bottom insets of the target container.
296      *
297      * @param parent the container in which to do the layout
298      * @return the preferred dimensions to lay out the
299      * subcomponents of the specified container
300      * @see java.awt.GridLayout#minimumLayoutSize
301      * @see java.awt.Container#getPreferredSize()
302      */

303     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc parent) {
304       synchronized (parent.getTreeLock()) {
305     Insets JavaDoc insets = parent.getInsets();
306     int ncomponents = parent.getComponentCount();
307     int nrows = rows;
308     int ncols = cols;
309
310     if (nrows > 0) {
311         ncols = (ncomponents + nrows - 1) / nrows;
312     } else {
313         nrows = (ncomponents + ncols - 1) / ncols;
314     }
315     int w = 0;
316     int h = 0;
317     for (int i = 0 ; i < ncomponents ; i++) {
318         Component JavaDoc comp = parent.getComponent(i);
319         Dimension JavaDoc d = comp.getPreferredSize();
320         if (w < d.width) {
321         w = d.width;
322         }
323         if (h < d.height) {
324         h = d.height;
325         }
326     }
327     return new Dimension JavaDoc(insets.left + insets.right + ncols*w + (ncols-1)*hgap,
328                  insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
329       }
330     }
331
332     /**
333      * Determines the minimum size of the container argument using this
334      * grid layout.
335      * <p>
336      * The minimum width of a grid layout is the largest minimum width
337      * of all of the components in the container times the number of columns,
338      * plus the horizontal padding times the number of columns minus one,
339      * plus the left and right insets of the target container.
340      * <p>
341      * The minimum height of a grid layout is the largest minimum height
342      * of all of the components in the container times the number of rows,
343      * plus the vertical padding times the number of rows minus one, plus
344      * the top and bottom insets of the target container.
345      *
346      * @param parent the container in which to do the layout
347      * @return the minimum dimensions needed to lay out the
348      * subcomponents of the specified container
349      * @see java.awt.GridLayout#preferredLayoutSize
350      * @see java.awt.Container#doLayout
351      */

352     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc parent) {
353       synchronized (parent.getTreeLock()) {
354         Insets JavaDoc insets = parent.getInsets();
355     int ncomponents = parent.getComponentCount();
356     int nrows = rows;
357     int ncols = cols;
358
359     if (nrows > 0) {
360         ncols = (ncomponents + nrows - 1) / nrows;
361     } else {
362         nrows = (ncomponents + ncols - 1) / ncols;
363     }
364     int w = 0;
365     int h = 0;
366     for (int i = 0 ; i < ncomponents ; i++) {
367         Component JavaDoc comp = parent.getComponent(i);
368         Dimension JavaDoc d = comp.getMinimumSize();
369         if (w < d.width) {
370         w = d.width;
371         }
372         if (h < d.height) {
373         h = d.height;
374         }
375     }
376     return new Dimension JavaDoc(insets.left + insets.right + ncols*w + (ncols-1)*hgap,
377                  insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
378       }
379     }
380
381     /**
382      * Lays out the specified container using this layout.
383      * <p>
384      * This method reshapes the components in the specified target
385      * container in order to satisfy the constraints of the
386      * <code>GridLayout</code> object.
387      * <p>
388      * The grid layout manager determines the size of individual
389      * components by dividing the free space in the container into
390      * equal-sized portions according to the number of rows and columns
391      * in the layout. The container's free space equals the container's
392      * size minus any insets and any specified horizontal or vertical
393      * gap. All components in a grid layout are given the same size.
394      *
395      * @param parent the container in which to do the layout
396      * @see java.awt.Container
397      * @see java.awt.Container#doLayout
398      */

399     public void layoutContainer(Container JavaDoc parent) {
400       synchronized (parent.getTreeLock()) {
401     Insets JavaDoc insets = parent.getInsets();
402     int ncomponents = parent.getComponentCount();
403     int nrows = rows;
404     int ncols = cols;
405     boolean ltr = parent.getComponentOrientation().isLeftToRight();
406
407     if (ncomponents == 0) {
408         return;
409     }
410     if (nrows > 0) {
411         ncols = (ncomponents + nrows - 1) / nrows;
412     } else {
413         nrows = (ncomponents + ncols - 1) / ncols;
414     }
415     int w = parent.width - (insets.left + insets.right);
416     int h = parent.height - (insets.top + insets.bottom);
417     w = (w - (ncols - 1) * hgap) / ncols;
418     h = (h - (nrows - 1) * vgap) / nrows;
419
420     if (ltr) {
421         for (int c = 0, x = insets.left ; c < ncols ; c++, x += w + hgap) {
422         for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {
423             int i = r * ncols + c;
424             if (i < ncomponents) {
425             parent.getComponent(i).setBounds(x, y, w, h);
426             }
427         }
428         }
429     } else {
430         for (int c = 0, x = parent.width - insets.right - w; c < ncols ; c++, x -= w + hgap) {
431         for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {
432             int i = r * ncols + c;
433             if (i < ncomponents) {
434             parent.getComponent(i).setBounds(x, y, w, h);
435             }
436         }
437         }
438     }
439       }
440     }
441     
442     /**
443      * Returns the string representation of this grid layout's values.
444      * @return a string representation of this grid layout
445      */

446     public String JavaDoc toString() {
447     return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap +
448                            ",rows=" + rows + ",cols=" + cols + "]";
449     }
450 }
451
Popular Tags