9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.awt;
27
28 /**
29 * The <code>GridLayout</code> class is a layout manager that
30 * lays out a container's components in a rectangular grid.
31 * The container is divided into equal-sized rectangles,
32 * and one component is placed in each rectangle.
33 * For example, the following is an applet that lays out six buttons
34 * into three rows and two columns:
35 *
36 * <hr><blockquote>
37 * <pre>
38 * import java.awt.*;
39 * import java.applet.Applet;
40 * public class ButtonGrid extends Applet {
41 * public void init() {
42 * setLayout(new GridLayout(3,2));
43 * add(new Button("1"));
44 * add(new Button("2"));
45 * add(new Button("3"));
46 * add(new Button("4"));
47 * add(new Button("5"));
48 * add(new Button("6"));
49 * }
50 * }
51 * </pre></blockquote><hr>
52 * <p>
53 * If the container's <code>ComponentOrientation</code> property is horizontal
54 * and left-to-right, the above example produces the output shown in Figure 1.
55 * If the container's <code>ComponentOrientation</code> property is horizontal
56 * and right-to-left, the example produces the output shown in Figure 2.
57 *
58 * <table style="float:center" WIDTH=600 summary="layout">
59 * <tr ALIGN=CENTER>
60 * <td><img SRC="doc-files/GridLayout-1.gif"
61 * alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 1 then 2.
62 * Row 2 shows buttons 3 then 4. Row 3 shows buttons 5 then 6.">
63 * </td>
64 *
65 * <td ALIGN=CENTER><img SRC="doc-files/GridLayout-2.gif"
66 * alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 2 then 1.
67 * Row 2 shows buttons 4 then 3. Row 3 shows buttons 6 then 5.">
68 * </td>
69 * </tr>
70 *
71 * <tr ALIGN=CENTER>
72 * <td>Figure 1: Horizontal, Left-to-Right</td>
73 *
74 * <td>Figure 2: Horizontal, Right-to-Left</td>
75 * </tr>
76 * </table>
77 * <p>
78 * When both the number of rows and the number of columns have
79 * been set to non-zero values, either by a constructor or
80 * by the <tt>setRows</tt> and <tt>setColumns</tt> methods, the number of
81 * columns specified is ignored. Instead, the number of
82 * columns is determined from the specified number of rows
83 * and the total number of components in the layout. So, for
84 * example, if three rows and two columns have been specified
85 * and nine components are added to the layout, they will
86 * be displayed as three rows of three columns. Specifying
87 * the number of columns affects the layout only when the
88 * number of rows is set to zero.
89 *
90 * @author Arthur van Hoff
91 * @since 1.0
92 */
93 public class GridLayout implements LayoutManager, java.io.Serializable {
94 /*
95 * serialVersionUID
96 */
97 private static final long serialVersionUID = -7411804673224730901L;
98
99 /**
100 * This is the horizontal gap (in pixels) which specifies the space
137 *
138 * @serial
139 * @see #getColumns()
140 * @see #setColumns(int)
141 */
142 int cols;
143
144 /**
145 * Creates a grid layout with a default of one column per component,
146 * in a single row.
147 * @since 1.1
148 */
149 public GridLayout() {
150 this(1, 0, 0, 0);
151 }
152
153 /**
154 * Creates a grid layout with the specified number of rows and
155 * columns. All components in the layout are given equal size.
156 * <p>
157 * One, but not both, of <code>rows</code> and <code>cols</code> can
158 * be zero, which means that any number of objects can be placed in a
159 * row or in a column.
160 * @param rows the rows, with the value zero meaning
161 * any number of rows.
162 * @param cols the columns, with the value zero meaning
163 * any number of columns.
164 */
165 public GridLayout(int rows, int cols) {
166 this(rows, cols, 0, 0);
167 }
168
169 /**
170 * Creates a grid layout with the specified number of rows and
171 * columns. All components in the layout are given equal size.
172 * <p>
173 * In addition, the horizontal and vertical gaps are set to the
174 * specified values. Horizontal gaps are placed between each
175 * of the columns. Vertical gaps are placed between each of
176 * the rows.
177 * <p>
178 * One, but not both, of <code>rows</code> and <code>cols</code> can
179 * be zero, which means that any number of objects can be placed in a
180 * row or in a column.
181 * <p>
182 * All <code>GridLayout</code> constructors defer to this one.
183 * @param rows the rows, with the value zero meaning
184 * any number of rows
185 * @param cols the columns, with the value zero meaning
186 * any number of columns
187 * @param hgap the horizontal gap
188 * @param vgap the vertical gap
189 * @exception IllegalArgumentException if the value of both
190 * <code>rows</code> and <code>cols</code> is
191 * set to zero
192 */
193 public GridLayout(int rows, int cols, int hgap, int vgap) {
194 if ((rows == 0) && (cols == 0)) {
195 throw new IllegalArgumentException("rows and cols cannot both be zero");
196 }
197 this.rows = rows;
198 this.cols = cols;
199 this.hgap = hgap;
200 this.vgap = vgap;
201 }
202
203 /**
204 * Gets the number of rows in this layout.
205 * @return the number of rows in this layout
206 * @since 1.1
207 */
208 public int getRows() {
209 return rows;
210 }
211
212 /**
213 * Sets the number of rows in this layout to the specified value.
214 * @param rows the number of rows in this layout
215 * @exception IllegalArgumentException if the value of both
216 * <code>rows</code> and <code>cols</code> is set to zero
217 * @since 1.1
218 */
219 public void setRows(int rows) {
220 if ((rows == 0) && (this.cols == 0)) {
221 throw new IllegalArgumentException("rows and cols cannot both be zero");
222 }
223 this.rows = rows;
224 }
225
226 /**
227 * Gets the number of columns in this layout.
228 * @return the number of columns in this layout
229 * @since 1.1
230 */
231 public int getColumns() {
232 return cols;
233 }
234
235 /**
236 * Sets the number of columns in this layout to the specified value.
237 * Setting the number of columns has no affect on the layout
238 * if the number of rows specified by a constructor or by
239 * the <tt>setRows</tt> method is non-zero. In that case, the number
240 * of columns displayed in the layout is determined by the total
241 * number of components and the number of rows specified.
242 * @param cols the number of columns in this layout
243 * @exception IllegalArgumentException if the value of both
244 * <code>rows</code> and <code>cols</code> is set to zero
245 * @since 1.1
246 */
247 public void setColumns(int cols) {
248 if ((cols == 0) && (this.rows == 0)) {
249 throw new IllegalArgumentException("rows and cols cannot both be zero");
250 }
251 this.cols = cols;
252 }
253
254 /**
255 * Gets the horizontal gap between components.
256 * @return the horizontal gap between components
257 * @since 1.1
258 */
259 public int getHgap() {
260 return hgap;
261 }
262
263 /**
264 * Sets the horizontal gap between components to the specified value.
388 for (int i = 0 ; i < ncomponents ; i++) {
389 Component comp = parent.getComponent(i);
390 Dimension d = comp.getMinimumSize();
391 if (w < d.width) {
392 w = d.width;
393 }
394 if (h < d.height) {
395 h = d.height;
396 }
397 }
398 return new Dimension(insets.left + insets.right + ncols*w + (ncols-1)*hgap,
399 insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
400 }
401 }
402
403 /**
404 * Lays out the specified container using this layout.
405 * <p>
406 * This method reshapes the components in the specified target
407 * container in order to satisfy the constraints of the
408 * <code>GridLayout</code> object.
409 * <p>
410 * The grid layout manager determines the size of individual
411 * components by dividing the free space in the container into
412 * equal-sized portions according to the number of rows and columns
413 * in the layout. The container's free space equals the container's
414 * size minus any insets and any specified horizontal or vertical
415 * gap. All components in a grid layout are given the same size.
416 *
417 * @param parent the container in which to do the layout
418 * @see java.awt.Container
419 * @see java.awt.Container#doLayout
420 */
421 public void layoutContainer(Container parent) {
422 synchronized (parent.getTreeLock()) {
423 Insets insets = parent.getInsets();
424 int ncomponents = parent.getComponentCount();
425 int nrows = rows;
426 int ncols = cols;
427 boolean ltr = parent.getComponentOrientation().isLeftToRight();
428
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.awt;
27
28 /**
29 * The {@code GridLayout} class is a layout manager that
30 * lays out a container's components in a rectangular grid.
31 * The container is divided into equal-sized rectangles,
32 * and one component is placed in each rectangle.
33 * For example, the following is an applet that lays out six buttons
34 * into three rows and two columns:
35 *
36 * <hr><blockquote>
37 * <pre>
38 * import java.awt.*;
39 * import java.applet.Applet;
40 * public class ButtonGrid extends Applet {
41 * public void init() {
42 * setLayout(new GridLayout(3,2));
43 * add(new Button("1"));
44 * add(new Button("2"));
45 * add(new Button("3"));
46 * add(new Button("4"));
47 * add(new Button("5"));
48 * add(new Button("6"));
49 * }
50 * }
51 * </pre></blockquote><hr>
52 * <p>
53 * If the container's {@code ComponentOrientation} property is horizontal
54 * and left-to-right, the above example produces the output shown in Figure 1.
55 * If the container's {@code ComponentOrientation} property is horizontal
56 * and right-to-left, the example produces the output shown in Figure 2.
57 *
58 * <table style="float:center" WIDTH=600 summary="layout">
59 * <tr ALIGN=CENTER>
60 * <td><img SRC="doc-files/GridLayout-1.gif"
61 * alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 1 then 2.
62 * Row 2 shows buttons 3 then 4. Row 3 shows buttons 5 then 6.">
63 * </td>
64 *
65 * <td ALIGN=CENTER><img SRC="doc-files/GridLayout-2.gif"
66 * alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 2 then 1.
67 * Row 2 shows buttons 4 then 3. Row 3 shows buttons 6 then 5.">
68 * </td>
69 * </tr>
70 *
71 * <tr ALIGN=CENTER>
72 * <td>Figure 1: Horizontal, Left-to-Right</td>
73 *
74 * <td>Figure 2: Horizontal, Right-to-Left</td>
75 * </tr>
76 * </table>
77 * <p>
78 * When both the number of rows and the number of columns have
79 * been set to non-zero values, either by a constructor or
80 * by the {@code setRows} and {@code setColumns} methods, the number of
81 * columns specified is ignored. Instead, the number of
82 * columns is determined from the specified number of rows
83 * and the total number of components in the layout. So, for
84 * example, if three rows and two columns have been specified
85 * and nine components are added to the layout, they will
86 * be displayed as three rows of three columns. Specifying
87 * the number of columns affects the layout only when the
88 * number of rows is set to zero.
89 *
90 * @author Arthur van Hoff
91 * @since 1.0
92 */
93 public class GridLayout implements LayoutManager, java.io.Serializable {
94 /*
95 * serialVersionUID
96 */
97 private static final long serialVersionUID = -7411804673224730901L;
98
99 /**
100 * This is the horizontal gap (in pixels) which specifies the space
137 *
138 * @serial
139 * @see #getColumns()
140 * @see #setColumns(int)
141 */
142 int cols;
143
144 /**
145 * Creates a grid layout with a default of one column per component,
146 * in a single row.
147 * @since 1.1
148 */
149 public GridLayout() {
150 this(1, 0, 0, 0);
151 }
152
153 /**
154 * Creates a grid layout with the specified number of rows and
155 * columns. All components in the layout are given equal size.
156 * <p>
157 * One, but not both, of {@code rows} and {@code cols} can
158 * be zero, which means that any number of objects can be placed in a
159 * row or in a column.
160 * @param rows the rows, with the value zero meaning
161 * any number of rows.
162 * @param cols the columns, with the value zero meaning
163 * any number of columns.
164 */
165 public GridLayout(int rows, int cols) {
166 this(rows, cols, 0, 0);
167 }
168
169 /**
170 * Creates a grid layout with the specified number of rows and
171 * columns. All components in the layout are given equal size.
172 * <p>
173 * In addition, the horizontal and vertical gaps are set to the
174 * specified values. Horizontal gaps are placed between each
175 * of the columns. Vertical gaps are placed between each of
176 * the rows.
177 * <p>
178 * One, but not both, of {@code rows} and {@code cols} can
179 * be zero, which means that any number of objects can be placed in a
180 * row or in a column.
181 * <p>
182 * All {@code GridLayout} constructors defer to this one.
183 * @param rows the rows, with the value zero meaning
184 * any number of rows
185 * @param cols the columns, with the value zero meaning
186 * any number of columns
187 * @param hgap the horizontal gap
188 * @param vgap the vertical gap
189 * @exception IllegalArgumentException if the value of both
190 * {@code rows} and {@code cols} is
191 * set to zero
192 */
193 public GridLayout(int rows, int cols, int hgap, int vgap) {
194 if ((rows == 0) && (cols == 0)) {
195 throw new IllegalArgumentException("rows and cols cannot both be zero");
196 }
197 this.rows = rows;
198 this.cols = cols;
199 this.hgap = hgap;
200 this.vgap = vgap;
201 }
202
203 /**
204 * Gets the number of rows in this layout.
205 * @return the number of rows in this layout
206 * @since 1.1
207 */
208 public int getRows() {
209 return rows;
210 }
211
212 /**
213 * Sets the number of rows in this layout to the specified value.
214 * @param rows the number of rows in this layout
215 * @exception IllegalArgumentException if the value of both
216 * {@code rows} and {@code cols} is set to zero
217 * @since 1.1
218 */
219 public void setRows(int rows) {
220 if ((rows == 0) && (this.cols == 0)) {
221 throw new IllegalArgumentException("rows and cols cannot both be zero");
222 }
223 this.rows = rows;
224 }
225
226 /**
227 * Gets the number of columns in this layout.
228 * @return the number of columns in this layout
229 * @since 1.1
230 */
231 public int getColumns() {
232 return cols;
233 }
234
235 /**
236 * Sets the number of columns in this layout to the specified value.
237 * Setting the number of columns has no affect on the layout
238 * if the number of rows specified by a constructor or by
239 * the {@code setRows} method is non-zero. In that case, the number
240 * of columns displayed in the layout is determined by the total
241 * number of components and the number of rows specified.
242 * @param cols the number of columns in this layout
243 * @exception IllegalArgumentException if the value of both
244 * {@code rows} and {@code cols} is set to zero
245 * @since 1.1
246 */
247 public void setColumns(int cols) {
248 if ((cols == 0) && (this.rows == 0)) {
249 throw new IllegalArgumentException("rows and cols cannot both be zero");
250 }
251 this.cols = cols;
252 }
253
254 /**
255 * Gets the horizontal gap between components.
256 * @return the horizontal gap between components
257 * @since 1.1
258 */
259 public int getHgap() {
260 return hgap;
261 }
262
263 /**
264 * Sets the horizontal gap between components to the specified value.
388 for (int i = 0 ; i < ncomponents ; i++) {
389 Component comp = parent.getComponent(i);
390 Dimension d = comp.getMinimumSize();
391 if (w < d.width) {
392 w = d.width;
393 }
394 if (h < d.height) {
395 h = d.height;
396 }
397 }
398 return new Dimension(insets.left + insets.right + ncols*w + (ncols-1)*hgap,
399 insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
400 }
401 }
402
403 /**
404 * Lays out the specified container using this layout.
405 * <p>
406 * This method reshapes the components in the specified target
407 * container in order to satisfy the constraints of the
408 * {@code GridLayout} object.
409 * <p>
410 * The grid layout manager determines the size of individual
411 * components by dividing the free space in the container into
412 * equal-sized portions according to the number of rows and columns
413 * in the layout. The container's free space equals the container's
414 * size minus any insets and any specified horizontal or vertical
415 * gap. All components in a grid layout are given the same size.
416 *
417 * @param parent the container in which to do the layout
418 * @see java.awt.Container
419 * @see java.awt.Container#doLayout
420 */
421 public void layoutContainer(Container parent) {
422 synchronized (parent.getTreeLock()) {
423 Insets insets = parent.getInsets();
424 int ncomponents = parent.getComponentCount();
425 int nrows = rows;
426 int ncols = cols;
427 boolean ltr = parent.getComponentOrientation().isLeftToRight();
428
|