KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > BorderFactory


1 /*
2  * @(#)BorderFactory.java 1.30 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 package javax.swing;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.Font JavaDoc;
11 import javax.swing.JComponent JavaDoc;
12 import javax.swing.border.*;
13
14 /**
15  * Factory class for vending standard <code>Border</code> objects. Wherever
16  * possible, this factory will hand out references to shared
17  * <code>Border</code> instances.
18  * For further information and examples see
19  * <a HREF="http://java.sun.com/docs/books/tutorial/uiswing/misc/border.html">How
20  to Use Borders</a>,
21  * a section in <em>The Java Tutorial</em>.
22  *
23  * @version 1.30 12/19/03
24  * @author David Kloba
25  */

26 public class BorderFactory
27 {
28
29     /** Don't let anyone instantiate this class */
30     private BorderFactory() {
31     }
32
33
34 //// LineBorder ///////////////////////////////////////////////////////////////
35
/**
36      * Creates a line border withe the specified color.
37      *
38      * @param color a <code>Color</code> to use for the line
39      * @return the <code>Border</code> object
40      */

41     public static Border createLineBorder(Color JavaDoc color) {
42         return new LineBorder(color, 1);
43     }
44
45     /**
46      * Creates a line border with the specified color
47      * and width. The width applies to all four sides of the
48      * border. To specify widths individually for the top,
49      * bottom, left, and right, use
50      * {@link #createMatteBorder(int,int,int,int,Color)}.
51      *
52      * @param color a <code>Color</code> to use for the line
53      * @param thickness an integer specifying the width in pixels
54      * @return the <code>Border</code> object
55      */

56     public static Border createLineBorder(Color JavaDoc color, int thickness) {
57         return new LineBorder(color, thickness);
58     }
59     
60 // public static Border createLineBorder(Color color, int thickness,
61
// boolean drawRounded) {
62
// return new JLineBorder(color, thickness, drawRounded);
63
// }
64

65 //// BevelBorder /////////////////////////////////////////////////////////////
66
///////////////////////////////////////////////////////////////////////////////
67
static final Border sharedRaisedBevel = new BevelBorder(BevelBorder.RAISED);
68     static final Border sharedLoweredBevel = new BevelBorder(BevelBorder.LOWERED);
69
70     /**
71      * Creates a border with a raised beveled edge, using
72      * brighter shades of the component's current background color
73      * for highlighting, and darker shading for shadows.
74      * (In a raised border, highlights are on top and shadows
75      * are underneath.)
76      *
77      * @return the <code>Border</code> object
78      */

79     public static Border createRaisedBevelBorder() {
80         return createSharedBevel(BevelBorder.RAISED);
81     }
82
83     /**
84      * Creates a border with a lowered beveled edge, using
85      * brighter shades of the component's current background color
86      * for highlighting, and darker shading for shadows.
87      * (In a lowered border, shadows are on top and highlights
88      * are underneath.)
89      *
90      * @return the <code>Border</code> object
91      */

92     public static Border createLoweredBevelBorder() {
93         return createSharedBevel(BevelBorder.LOWERED);
94     }
95
96     /**
97      * Creates a beveled border of the specified type, using
98      * brighter shades of the component's current background color
99      * for highlighting, and darker shading for shadows.
100      * (In a lowered border, shadows are on top and highlights
101      * are underneath.)
102      *
103      * @param type an integer specifying either
104      * <code>BevelBorder.LOWERED</code> or
105      * <code>BevelBorder.RAISED</code>
106      * @return the <code>Border</code> object
107      */

108     public static Border createBevelBorder(int type) {
109     return createSharedBevel(type);
110     }
111     
112     /**
113      * Creates a beveled border of the specified type, using
114      * the specified highlighting and shadowing. The outer
115      * edge of the highlighted area uses a brighter shade of
116      * the highlight color. The inner edge of the shadow area
117      * uses a brighter shade of the shadow color.
118      *
119      * @param type an integer specifying either
120      * <code>BevelBorder.LOWERED</code> or
121      * <code>BevelBorder.RAISED</code>
122      * @param highlight a <code>Color</code> object for highlights
123      * @param shadow a <code>Color</code> object for shadows
124      * @return the <code>Border</code> object
125      */

126     public static Border createBevelBorder(int type, Color JavaDoc highlight, Color JavaDoc shadow) {
127         return new BevelBorder(type, highlight, shadow);
128     }
129
130     /**
131      * Creates a beveled border of the specified type, using
132      * the specified colors for the inner and outer highlight
133      * and shadow areas.
134      * <p>
135      * Note: The shadow inner and outer colors are
136      * switched for a lowered bevel border.
137      *
138      * @param type an integer specifying either
139      * <code>BevelBorder.LOWERED</code> or
140      * <code>BevelBorder.RAISED</code>
141      * @param highlightOuter a <code>Color</code> object for the
142      * outer edge of the highlight area
143      * @param highlightInner a <code>Color</code> object for the
144      * inner edge of the highlight area
145      * @param shadowOuter a <code>Color</code> object for the
146      * outer edge of the shadow area
147      * @param shadowInner a <code>Color</code> object for the
148      * inner edge of the shadow area
149      * @return the <code>Border</code> object
150      */

151     public static Border createBevelBorder(int type,
152                         Color JavaDoc highlightOuter, Color JavaDoc highlightInner,
153                         Color JavaDoc shadowOuter, Color JavaDoc shadowInner) {
154         return new BevelBorder(type, highlightOuter, highlightInner,
155                     shadowOuter, shadowInner);
156     }
157
158     static Border createSharedBevel(int type) {
159     if(type == BevelBorder.RAISED) {
160         return sharedRaisedBevel;
161     } else if(type == BevelBorder.LOWERED) {
162         return sharedLoweredBevel;
163     }
164     return null;
165     }
166 //// EtchedBorder ///////////////////////////////////////////////////////////
167
static final Border sharedEtchedBorder = new EtchedBorder();
168     private static Border sharedRaisedEtchedBorder;
169
170     /**
171      * Creates a border with an "etched" look using
172      * the component's current background color for
173      * highlighting and shading.
174      *
175      * @return the <code>Border</code> object
176      */

177     public static Border createEtchedBorder() {
178     return sharedEtchedBorder;
179     }
180
181     /**
182      * Creates a border with an "etched" look using
183      * the specified highlighting and shading colors.
184      *
185      * @param highlight a <code>Color</code> object for the border highlights
186      * @param shadow a <code>Color</code> object for the border shadows
187      * @return the <code>Border</code> object
188      */

189     public static Border createEtchedBorder(Color JavaDoc highlight, Color JavaDoc shadow) {
190         return new EtchedBorder(highlight, shadow);
191     }
192
193     /**
194      * Creates a border with an "etched" look using
195      * the component's current background color for
196      * highlighting and shading.
197      *
198      * @param type one of <code>EtchedBorder.RAISED</code>, or
199      * <code>EtchedBorder.LOWERED</code>
200      * @return the <code>Border</code> object
201      * @exception IllegalArgumentException if type is not either
202      * <code>EtchedBorder.RAISED</code> or
203      * <code>EtchedBorder.LOWERED</code>
204      * @since 1.3
205      */

206     public static Border createEtchedBorder(int type) {
207     switch (type) {
208     case EtchedBorder.RAISED:
209         if (sharedRaisedEtchedBorder == null) {
210         sharedRaisedEtchedBorder = new EtchedBorder
211                                    (EtchedBorder.RAISED);
212         }
213         return sharedRaisedEtchedBorder;
214     case EtchedBorder.LOWERED:
215         return sharedEtchedBorder;
216     default:
217         throw new IllegalArgumentException JavaDoc("type must be one of EtchedBorder.RAISED or EtchedBorder.LOWERED");
218     }
219     }
220
221     /**
222      * Creates a border with an "etched" look using
223      * the specified highlighting and shading colors.
224      *
225      * @param type one of <code>EtchedBorder.RAISED</code>, or
226      * <code>EtchedBorder.LOWERED</code>
227      * @param highlight a <code>Color</code> object for the border highlights
228      * @param shadow a <code>Color</code> object for the border shadows
229      * @return the <code>Border</code> object
230      * @since 1.3
231      */

232     public static Border createEtchedBorder(int type, Color JavaDoc highlight,
233                         Color JavaDoc shadow) {
234         return new EtchedBorder(type, highlight, shadow);
235     }
236
237 //// TitledBorder ////////////////////////////////////////////////////////////
238
/**
239      * Creates a new title border specifying the text of the title, using
240      * the default border (etched), using the default text position
241      * (sitting on the top
242      * line) and default justification (leading) and using the default
243      * font and text color determined by the current look and feel.
244      *
245      * @param title a <code>String</code> containing the text of the title
246      * @return the <code>TitledBorder</code> object
247      */

248     public static TitledBorder createTitledBorder(String JavaDoc title) {
249         return new TitledBorder(title);
250     }
251
252     /**
253      * Creates a new title border with an empty title specifying the
254      * border object, using the default text position (sitting on the top
255      * line) and default justification (leading) and using the default
256      * font, and text color.
257      *
258      * @param border the <code>Border</code> object to add the title to, if
259      * null the <code>Border</code> is determined by the
260      * current look and feel.
261      * @return the <code>TitledBorder</code> object
262      */

263     public static TitledBorder createTitledBorder(Border border) {
264         return new TitledBorder(border);
265     }
266
267     /**
268      * Adds a title to an existing border, specifying the text of
269      * the title, using the default positioning (sitting on the top
270      * line) and default justification (leading) and using the default
271      * font and text color determined by the current look and feel.
272      *
273      * @param border the <code>Border</code> object to add the title to
274      * @param title a <code>String</code> containing the text of the title
275      * @return the <code>TitledBorder</code> object
276      */

277     public static TitledBorder createTitledBorder(Border border,
278                            String JavaDoc title) {
279         return new TitledBorder(border, title);
280     }
281
282     /**
283      * Adds a title to an existing border, specifying the text of
284      * the title along with its positioning, using the default
285      * font and text color determined by the current look and feel.
286      *
287      * @param border the <code>Border</code> object to add the title to
288      * @param title a <code>String</code> containing the text of the title
289      * @param titleJustification an integer specifying the justification
290      * of the title -- one of the following:
291      *<ul>
292      *<li><code>TitledBorder.LEFT</code>
293      *<li><code>TitledBorder.CENTER</code>
294      *<li><code>TitledBorder.RIGHT</code>
295      *<li><code>TitledBorder.LEADING</code>
296      *<li><code>TitledBorder.TRAILING<code>
297      *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading)
298      *</ul>
299      * @param titlePosition an integer specifying the vertical position of
300      * the text in relation to the border -- one of the following:
301      *<ul>
302      *<li><code> TitledBorder.ABOVE_TOP</code>
303      *<li>TitledBorder.TOP</code> (sitting on the top line)
304      *<li><code>TitledBorder.BELOW_TOP</code>
305      *<li><code>TitledBorder.ABOVE_BOTTOM</code>
306      *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line)
307      *<li><code>TitledBorder.BELOW_BOTTOM</code>
308      *<li><code>TitledBorder.DEFAULT_POSITION</code> (top)
309      *</ul>
310      * @return the <code>TitledBorder</code> object
311      */

312     public static TitledBorder createTitledBorder(Border border,
313                         String JavaDoc title,
314                         int titleJustification,
315                         int titlePosition) {
316         return new TitledBorder(border, title, titleJustification,
317                         titlePosition);
318     }
319
320     /**
321      * Adds a title to an existing border, specifying the text of
322      * the title along with its positioning and font, using the
323      * default text color determined by the current look and feel.
324      *
325      * @param border the <code>Border</code> object to add the title to
326      * @param title a <code>String</code> containing the text of the title
327      * @param titleJustification an integer specifying the justification
328      * of the title -- one of the following:
329      *<ul>
330      *<li><code>TitledBorder.LEFT</code>
331      *<li><code>TitledBorder.CENTER</code>
332      *<li><code>TitledBorder.RIGHT</code>
333      *<li><code>TitledBorder.LEADING</code>
334      *<li><code>TitledBorder.TRAILING<code>
335      *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading)
336      *</ul>
337      * @param titlePosition an integer specifying the vertical position of
338      * the text in relation to the border -- one of the following:
339      *<ul>
340      *<li><code> TitledBorder.ABOVE_TOP</code>
341      *<li>TitledBorder.TOP</code> (sitting on the top line)
342      *<li><code>TitledBorder.BELOW_TOP</code>
343      *<li><code>TitledBorder.ABOVE_BOTTOM</code>
344      *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line)
345      *<li><code>TitledBorder.BELOW_BOTTOM</code>
346      *<li><code>TitledBorder.DEFAULT_POSITION</code> (top)
347      *</ul>
348      * @param titleFont a Font object specifying the title font
349      * @return the TitledBorder object
350      */

351     public static TitledBorder createTitledBorder(Border border,
352                         String JavaDoc title,
353                         int titleJustification,
354                         int titlePosition,
355                         Font JavaDoc titleFont) {
356         return new TitledBorder(border, title, titleJustification,
357                         titlePosition, titleFont);
358     }
359
360     /**
361      * Adds a title to an existing border, specifying the text of
362      * the title along with its positioning, font, and color.
363      *
364      * @param border the <code>Border</code> object to add the title to
365      * @param title a <code>String</code> containing the text of the title
366      * @param titleJustification an integer specifying the justification
367      * of the title -- one of the following:
368      *<ul>
369      *<li><code>TitledBorder.LEFT</code>
370      *<li><code>TitledBorder.CENTER</code>
371      *<li><code>TitledBorder.RIGHT</code>
372      *<li><code>TitledBorder.LEADING</code>
373      *<li><code>TitledBorder.TRAILING<code>
374      *<li><code>TitledBorder.DEFAULT_JUSTIFICATION</code> (leading)
375      *</ul>
376      * @param titlePosition an integer specifying the vertical position of
377      * the text in relation to the border -- one of the following:
378      *<ul>
379      *<li><code> TitledBorder.ABOVE_TOP</code>
380      *<li>TitledBorder.TOP</code> (sitting on the top line)
381      *<li><code>TitledBorder.BELOW_TOP</code>
382      *<li><code>TitledBorder.ABOVE_BOTTOM</code>
383      *<li><code>TitledBorder.BOTTOM</code> (sitting on the bottom line)
384      *<li><code>TitledBorder.BELOW_BOTTOM</code>
385      *<li><code>TitledBorder.DEFAULT_POSITION</code> (top)
386      *</ul>
387      * @param titleFont a <code>Font</code> object specifying the title font
388      * @param titleColor a <code>Color</code> object specifying the title color
389      * @return the <code>TitledBorder</code> object
390      */

391     public static TitledBorder createTitledBorder(Border border,
392                         String JavaDoc title,
393                         int titleJustification,
394                         int titlePosition,
395                         Font JavaDoc titleFont,
396                         Color JavaDoc titleColor) {
397         return new TitledBorder(border, title, titleJustification,
398                         titlePosition, titleFont, titleColor);
399     }
400 //// EmptyBorder ///////////////////////////////////////////////////////////
401
final static Border emptyBorder = new EmptyBorder(0, 0, 0, 0);
402
403     /**
404      * Creates an empty border that takes up no space. (The width
405      * of the top, bottom, left, and right sides are all zero.)
406      *
407      * @return the <code>Border</code> object
408      */

409     public static Border createEmptyBorder() {
410     return emptyBorder;
411     }
412
413     /**
414      * Creates an empty border that takes up space but which does
415      * no drawing, specifying the width of the top, left, bottom, and
416      * right sides.
417      *
418      * @param top an integer specifying the width of the top,
419      * in pixels
420      * @param left an integer specifying the width of the left side,
421      * in pixels
422      * @param bottom an integer specifying the width of the bottom,
423      * in pixels
424      * @param right an integer specifying the width of the right side,
425      * in pixels
426      * @return the <code>Border</code> object
427      */

428     public static Border createEmptyBorder(int top, int left,
429                         int bottom, int right) {
430     return new EmptyBorder(top, left, bottom, right);
431     }
432
433 //// CompoundBorder ////////////////////////////////////////////////////////
434
/**
435      * Creates a compound border with a <code>null</code> inside edge and a
436      * <code>null</code> outside edge.
437      *
438      * @return the <code>CompoundBorder</code> object
439      */

440     public static CompoundBorder createCompoundBorder() {
441     return new CompoundBorder();
442     }
443
444     /**
445      * Creates a compound border specifying the border objects to use
446      * for the outside and inside edges.
447      *
448      * @param outsideBorder a <code>Border</code> object for the outer
449      * edge of the compound border
450      * @param insideBorder a <code>Border</code> object for the inner
451      * edge of the compound border
452      * @return the <code>CompoundBorder</code> object
453      */

454     public static CompoundBorder createCompoundBorder(Border outsideBorder,
455                         Border insideBorder) {
456     return new CompoundBorder(outsideBorder, insideBorder);
457     }
458
459 //// MatteBorder ////////////////////////////////////////////////////////
460
/**
461      * Creates a matte-look border using a solid color. (The difference between
462      * this border and a line border is that you can specify the individual
463      * border dimensions.)
464      *
465      * @param top an integer specifying the width of the top,
466      * in pixels
467      * @param left an integer specifying the width of the left side,
468      * in pixels
469      * @param bottom an integer specifying the width of the right side,
470      * in pixels
471      * @param right an integer specifying the width of the bottom,
472      * in pixels
473      * @param color a <code>Color</code> to use for the border
474      * @return the <code>MatteBorder</code> object
475      */

476     public static MatteBorder createMatteBorder(int top, int left, int bottom, int right,
477                                                 Color JavaDoc color) {
478         return new MatteBorder(top, left, bottom, right, color);
479     }
480
481     /**
482      * Creates a matte-look border that consists of multiple tiles of a
483      * specified icon. Multiple copies of the icon are placed side-by-side
484      * to fill up the border area.
485      * <p>
486      * Note:<br>
487      * If the icon doesn't load, the border area is painted gray.
488      *
489      * @param top an integer specifying the width of the top,
490      * in pixels
491      * @param left an integer specifying the width of the left side,
492      * in pixels
493      * @param bottom an integer specifying the width of the right side,
494      * in pixels
495      * @param right an integer specifying the width of the bottom,
496      * in pixels
497      * @param tileIcon the <code>Icon</code> object used for the border tiles
498      * @return the <code>MatteBorder</code> object
499      */

500     public static MatteBorder createMatteBorder(int top, int left, int bottom, int right,
501                                                 Icon JavaDoc tileIcon) {
502         return new MatteBorder(top, left, bottom, right, tileIcon);
503     }
504 }
505
Popular Tags