Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/javax/swing/Box.java
+++ new/src/share/classes/javax/swing/Box.java
1 1 /*
2 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26
27 27 package javax.swing;
28 28
29 29 import java.awt.*;
30 30 import java.awt.event.*;
31 31 import java.beans.ConstructorProperties;
32 32 import java.util.Locale;
33 33 import java.io.Serializable;
34 34 import javax.accessibility.*;
35 35
36 36 /**
37 37 * A lightweight container
38 38 * that uses a BoxLayout object as its layout manager.
39 39 * Box provides several class methods
40 40 * that are useful for containers using BoxLayout --
41 41 * even non-Box containers.
42 42 *
43 43 * <p>
44 44 * The <code>Box</code> class can create several kinds
45 45 * of invisible components
46 46 * that affect layout:
47 47 * glue, struts, and rigid areas.
48 48 * If all the components your <code>Box</code> contains
49 49 * have a fixed size,
50 50 * you might want to use a glue component
51 51 * (returned by <code>createGlue</code>)
52 52 * to control the components' positions.
↓ open down ↓ |
52 lines elided |
↑ open up ↑ |
53 53 * If you need a fixed amount of space between two components,
54 54 * try using a strut
55 55 * (<code>createHorizontalStrut</code> or <code>createVerticalStrut</code>).
56 56 * If you need an invisible component
57 57 * that always takes up the same amount of space,
58 58 * get it by invoking <code>createRigidArea</code>.
59 59 * <p>
60 60 * If you are implementing a <code>BoxLayout</code> you
61 61 * can find further information and examples in
62 62 * <a
63 - href="http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html">How to Use BoxLayout</a>,
63 + href="https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html">How to Use BoxLayout</a>,
64 64 * a section in <em>The Java Tutorial.</em>
65 65 * <p>
66 66 * <strong>Warning:</strong>
67 67 * Serialized objects of this class will not be compatible with
68 68 * future Swing releases. The current serialization support is
69 69 * appropriate for short term storage or RMI between applications running
70 70 * the same version of Swing. As of 1.4, support for long term storage
71 71 * of all JavaBeans™
72 72 * has been added to the <code>java.beans</code> package.
73 73 * Please see {@link java.beans.XMLEncoder}.
74 74 *
75 75 * @see BoxLayout
76 76 *
77 77 * @author Timothy Prinzing
78 78 */
79 79 @SuppressWarnings("serial")
80 80 public class Box extends JComponent implements Accessible {
81 81
82 82 /**
83 83 * Creates a <code>Box</code> that displays its components
84 84 * along the the specified axis.
85 85 *
86 86 * @param axis can be {@link BoxLayout#X_AXIS},
87 87 * {@link BoxLayout#Y_AXIS},
88 88 * {@link BoxLayout#LINE_AXIS} or
89 89 * {@link BoxLayout#PAGE_AXIS}.
90 90 * @throws AWTError if the <code>axis</code> is invalid
91 91 * @see #createHorizontalBox
92 92 * @see #createVerticalBox
93 93 */
94 94 public Box(int axis) {
95 95 super();
96 96 super.setLayout(new BoxLayout(this, axis));
97 97 }
98 98
99 99 /**
100 100 * Creates a <code>Box</code> that displays its components
101 101 * from left to right. If you want a <code>Box</code> that
102 102 * respects the component orientation you should create the
103 103 * <code>Box</code> using the constructor and pass in
104 104 * <code>BoxLayout.LINE_AXIS</code>, eg:
105 105 * <pre>
106 106 * Box lineBox = new Box(BoxLayout.LINE_AXIS);
107 107 * </pre>
108 108 *
109 109 * @return the box
110 110 */
111 111 public static Box createHorizontalBox() {
112 112 return new Box(BoxLayout.X_AXIS);
113 113 }
114 114
115 115 /**
116 116 * Creates a <code>Box</code> that displays its components
117 117 * from top to bottom. If you want a <code>Box</code> that
118 118 * respects the component orientation you should create the
119 119 * <code>Box</code> using the constructor and pass in
120 120 * <code>BoxLayout.PAGE_AXIS</code>, eg:
121 121 * <pre>
122 122 * Box lineBox = new Box(BoxLayout.PAGE_AXIS);
123 123 * </pre>
124 124 *
125 125 * @return the box
126 126 */
127 127 public static Box createVerticalBox() {
128 128 return new Box(BoxLayout.Y_AXIS);
129 129 }
130 130
131 131 /**
132 132 * Creates an invisible component that's always the specified size.
133 133 * <!-- WHEN WOULD YOU USE THIS AS OPPOSED TO A STRUT? -->
134 134 *
135 135 * @param d the dimensions of the invisible component
136 136 * @return the component
137 137 * @see #createGlue
138 138 * @see #createHorizontalStrut
139 139 * @see #createVerticalStrut
140 140 */
141 141 public static Component createRigidArea(Dimension d) {
142 142 return new Filler(d, d, d);
143 143 }
144 144
145 145 /**
146 146 * Creates an invisible, fixed-width component.
147 147 * In a horizontal box,
148 148 * you typically use this method
149 149 * to force a certain amount of space between two components.
150 150 * In a vertical box,
151 151 * you might use this method
152 152 * to force the box to be at least the specified width.
153 153 * The invisible component has no height
154 154 * unless excess space is available,
155 155 * in which case it takes its share of available space,
156 156 * just like any other component that has no maximum height.
157 157 *
158 158 * @param width the width of the invisible component, in pixels >= 0
159 159 * @return the component
160 160 * @see #createVerticalStrut
161 161 * @see #createGlue
162 162 * @see #createRigidArea
163 163 */
164 164 public static Component createHorizontalStrut(int width) {
165 165 return new Filler(new Dimension(width,0), new Dimension(width,0),
166 166 new Dimension(width, Short.MAX_VALUE));
167 167 }
168 168
169 169 /**
170 170 * Creates an invisible, fixed-height component.
171 171 * In a vertical box,
172 172 * you typically use this method
173 173 * to force a certain amount of space between two components.
174 174 * In a horizontal box,
175 175 * you might use this method
176 176 * to force the box to be at least the specified height.
177 177 * The invisible component has no width
178 178 * unless excess space is available,
179 179 * in which case it takes its share of available space,
180 180 * just like any other component that has no maximum width.
181 181 *
182 182 * @param height the height of the invisible component, in pixels >= 0
183 183 * @return the component
184 184 * @see #createHorizontalStrut
185 185 * @see #createGlue
186 186 * @see #createRigidArea
187 187 */
188 188 public static Component createVerticalStrut(int height) {
189 189 return new Filler(new Dimension(0,height), new Dimension(0,height),
190 190 new Dimension(Short.MAX_VALUE, height));
191 191 }
192 192
193 193 /**
194 194 * Creates an invisible "glue" component
195 195 * that can be useful in a Box
196 196 * whose visible components have a maximum width
197 197 * (for a horizontal box)
198 198 * or height (for a vertical box).
199 199 * You can think of the glue component
200 200 * as being a gooey substance
201 201 * that expands as much as necessary
202 202 * to fill the space between its neighboring components.
203 203 *
204 204 * <p>
205 205 *
206 206 * For example, suppose you have
207 207 * a horizontal box that contains two fixed-size components.
208 208 * If the box gets extra space,
209 209 * the fixed-size components won't become larger,
210 210 * so where does the extra space go?
211 211 * Without glue,
212 212 * the extra space goes to the right of the second component.
213 213 * If you put glue between the fixed-size components,
214 214 * then the extra space goes there.
215 215 * If you put glue before the first fixed-size component,
216 216 * the extra space goes there,
217 217 * and the fixed-size components are shoved against the right
218 218 * edge of the box.
219 219 * If you put glue before the first fixed-size component
220 220 * and after the second fixed-size component,
221 221 * the fixed-size components are centered in the box.
222 222 *
223 223 * <p>
224 224 *
225 225 * To use glue,
226 226 * call <code>Box.createGlue</code>
227 227 * and add the returned component to a container.
228 228 * The glue component has no minimum or preferred size,
229 229 * so it takes no space unless excess space is available.
230 230 * If excess space is available,
231 231 * then the glue component takes its share of available
232 232 * horizontal or vertical space,
233 233 * just like any other component that has no maximum width or height.
234 234 *
235 235 * @return the component
236 236 */
237 237 public static Component createGlue() {
238 238 return new Filler(new Dimension(0,0), new Dimension(0,0),
239 239 new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
240 240 }
241 241
242 242 /**
243 243 * Creates a horizontal glue component.
244 244 *
245 245 * @return the component
246 246 */
247 247 public static Component createHorizontalGlue() {
248 248 return new Filler(new Dimension(0,0), new Dimension(0,0),
249 249 new Dimension(Short.MAX_VALUE, 0));
250 250 }
251 251
252 252 /**
253 253 * Creates a vertical glue component.
254 254 *
255 255 * @return the component
256 256 */
257 257 public static Component createVerticalGlue() {
258 258 return new Filler(new Dimension(0,0), new Dimension(0,0),
259 259 new Dimension(0, Short.MAX_VALUE));
260 260 }
261 261
262 262 /**
263 263 * Throws an AWTError, since a Box can use only a BoxLayout.
264 264 *
265 265 * @param l the layout manager to use
266 266 */
267 267 public void setLayout(LayoutManager l) {
268 268 throw new AWTError("Illegal request");
269 269 }
270 270
271 271 /**
272 272 * Paints this <code>Box</code>. If this <code>Box</code> has a UI this
273 273 * method invokes super's implementation, otherwise if this
274 274 * <code>Box</code> is opaque the <code>Graphics</code> is filled
275 275 * using the background.
276 276 *
277 277 * @param g the <code>Graphics</code> to paint to
278 278 * @throws NullPointerException if <code>g</code> is null
279 279 * @since 1.6
280 280 */
281 281 protected void paintComponent(Graphics g) {
282 282 if (ui != null) {
283 283 // On the off chance some one created a UI, honor it
284 284 super.paintComponent(g);
285 285 } else if (isOpaque()) {
286 286 g.setColor(getBackground());
287 287 g.fillRect(0, 0, getWidth(), getHeight());
288 288 }
289 289 }
290 290
291 291
292 292 /**
293 293 * An implementation of a lightweight component that participates in
294 294 * layout but has no view.
295 295 * <p>
296 296 * <strong>Warning:</strong>
297 297 * Serialized objects of this class will not be compatible with
298 298 * future Swing releases. The current serialization support is
299 299 * appropriate for short term storage or RMI between applications running
300 300 * the same version of Swing. As of 1.4, support for long term storage
301 301 * of all JavaBeans™
302 302 * has been added to the <code>java.beans</code> package.
303 303 * Please see {@link java.beans.XMLEncoder}.
304 304 */
305 305 @SuppressWarnings("serial")
306 306 public static class Filler extends JComponent implements Accessible {
307 307
308 308 /**
309 309 * Constructor to create shape with the given size ranges.
310 310 *
311 311 * @param min Minimum size
312 312 * @param pref Preferred size
313 313 * @param max Maximum size
314 314 */
315 315 @ConstructorProperties({"minimumSize", "preferredSize", "maximumSize"})
316 316 public Filler(Dimension min, Dimension pref, Dimension max) {
317 317 setMinimumSize(min);
318 318 setPreferredSize(pref);
319 319 setMaximumSize(max);
320 320 }
321 321
322 322 /**
323 323 * Change the size requests for this shape. An invalidate() is
324 324 * propagated upward as a result so that layout will eventually
325 325 * happen with using the new sizes.
326 326 *
327 327 * @param min Value to return for getMinimumSize
328 328 * @param pref Value to return for getPreferredSize
329 329 * @param max Value to return for getMaximumSize
330 330 */
331 331 public void changeShape(Dimension min, Dimension pref, Dimension max) {
332 332 setMinimumSize(min);
333 333 setPreferredSize(pref);
334 334 setMaximumSize(max);
335 335 revalidate();
336 336 }
337 337
338 338 // ---- Component methods ------------------------------------------
339 339
340 340 /**
341 341 * Paints this <code>Filler</code>. If this
342 342 * <code>Filler</code> has a UI this method invokes super's
343 343 * implementation, otherwise if this <code>Filler</code> is
344 344 * opaque the <code>Graphics</code> is filled using the
345 345 * background.
346 346 *
347 347 * @param g the <code>Graphics</code> to paint to
348 348 * @throws NullPointerException if <code>g</code> is null
349 349 * @since 1.6
350 350 */
351 351 protected void paintComponent(Graphics g) {
352 352 if (ui != null) {
353 353 // On the off chance some one created a UI, honor it
354 354 super.paintComponent(g);
355 355 } else if (isOpaque()) {
356 356 g.setColor(getBackground());
357 357 g.fillRect(0, 0, getWidth(), getHeight());
358 358 }
359 359 }
360 360
361 361 /////////////////
362 362 // Accessibility support for Box$Filler
363 363 ////////////////
364 364
365 365 /**
366 366 * Gets the AccessibleContext associated with this Box.Filler.
367 367 * For box fillers, the AccessibleContext takes the form of an
368 368 * AccessibleBoxFiller.
369 369 * A new AccessibleAWTBoxFiller instance is created if necessary.
370 370 *
371 371 * @return an AccessibleBoxFiller that serves as the
372 372 * AccessibleContext of this Box.Filler.
373 373 */
374 374 public AccessibleContext getAccessibleContext() {
375 375 if (accessibleContext == null) {
376 376 accessibleContext = new AccessibleBoxFiller();
377 377 }
378 378 return accessibleContext;
379 379 }
380 380
381 381 /**
382 382 * This class implements accessibility support for the
383 383 * <code>Box.Filler</code> class.
384 384 */
385 385 @SuppressWarnings("serial")
386 386 protected class AccessibleBoxFiller extends AccessibleAWTComponent {
387 387 // AccessibleContext methods
388 388 //
389 389 /**
390 390 * Gets the role of this object.
391 391 *
392 392 * @return an instance of AccessibleRole describing the role of
393 393 * the object (AccessibleRole.FILLER)
394 394 * @see AccessibleRole
395 395 */
396 396 public AccessibleRole getAccessibleRole() {
397 397 return AccessibleRole.FILLER;
398 398 }
399 399 }
400 400 }
401 401
402 402 /////////////////
403 403 // Accessibility support for Box
404 404 ////////////////
405 405
406 406 /**
407 407 * Gets the AccessibleContext associated with this Box.
408 408 * For boxes, the AccessibleContext takes the form of an
409 409 * AccessibleBox.
410 410 * A new AccessibleAWTBox instance is created if necessary.
411 411 *
412 412 * @return an AccessibleBox that serves as the
413 413 * AccessibleContext of this Box
414 414 */
415 415 public AccessibleContext getAccessibleContext() {
416 416 if (accessibleContext == null) {
417 417 accessibleContext = new AccessibleBox();
418 418 }
419 419 return accessibleContext;
420 420 }
421 421
422 422 /**
423 423 * This class implements accessibility support for the
424 424 * <code>Box</code> class.
425 425 */
426 426 @SuppressWarnings("serial")
427 427 protected class AccessibleBox extends AccessibleAWTContainer {
428 428 // AccessibleContext methods
429 429 //
430 430 /**
431 431 * Gets the role of this object.
432 432 *
433 433 * @return an instance of AccessibleRole describing the role of the
434 434 * object (AccessibleRole.FILLER)
435 435 * @see AccessibleRole
436 436 */
437 437 public AccessibleRole getAccessibleRole() {
438 438 return AccessibleRole.FILLER;
439 439 }
440 440 } // inner class AccessibleBox
441 441 }
↓ open down ↓ |
368 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX