Create embedded-5_0-branch branch for development on ARM embedded cores.
[official-gcc.git] / embedded-5_0-branch / libjava / classpath / javax / swing / border / TitledBorder.java
blob4d4f3af7d26b4871a7c1e399c420fa59cdd4c0dd
1 /* TitledBorder.java --
2 Copyright (C) 2003, 2004, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.border;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.Dimension;
44 import java.awt.Font;
45 import java.awt.FontMetrics;
46 import java.awt.Graphics;
47 import java.awt.Insets;
48 import java.awt.Point;
49 import java.awt.Rectangle;
51 import javax.swing.SwingUtilities;
52 import javax.swing.UIManager;
55 /**
56 * A border that paints a title on top of another border.
58 * @author Sascha Brawer (brawer@dandelis.ch)
60 public class TitledBorder extends AbstractBorder
62 /**
63 * A value for the <code>titlePosition</code> property that vertically
64 * positions the title text at the default vertical position, which
65 * is in the middle of the top line of the border.
67 * @see #getTitlePosition()
68 * @see #setTitlePosition(int)
70 public static final int DEFAULT_POSITION = 0;
73 /**
74 * A value for the <code>titlePosition</code> property that vertically
75 * positions the title text above the top line of the border.
77 * @see #getTitlePosition()
78 * @see #setTitlePosition(int)
80 public static final int ABOVE_TOP = 1;
83 /**
84 * A value for the <code>titlePosition</code> property that vertically
85 * positions the title text at the middle of the top line
86 * of the border.
88 * @see #getTitlePosition()
89 * @see #setTitlePosition(int)
91 public static final int TOP = 2;
94 /**
95 * A value for the <code>titlePosition</code> property that vertically
96 * positions the title text below the top line of the border.
98 * @see #getTitlePosition()
99 * @see #setTitlePosition(int)
101 public static final int BELOW_TOP = 3;
105 * A value for the <code>titlePosition</code> property that vertically
106 * positions the title text above the bottom line of the border.
108 * @see #getTitlePosition()
109 * @see #setTitlePosition(int)
111 public static final int ABOVE_BOTTOM = 4;
115 * A value for the <code>titlePosition</code> property that vertically
116 * positions the title text at the center of the bottom line
117 * of the border.
119 * @see #getTitlePosition()
120 * @see #setTitlePosition(int)
122 public static final int BOTTOM = 5;
126 * A value for the <code>titlePosition</code> property that vertically
127 * positions the title text below the bottom line of the border.
129 * @see #getTitlePosition()
130 * @see #setTitlePosition(int)
132 public static final int BELOW_BOTTOM = 6;
136 * A value for the <code>titleJustification</code> property that
137 * horizontally aligns the title text with either the left or the
138 * right edge of the border, depending on the orientation of the
139 * component nested into the border. If the component orientation
140 * is left-to-right, the title text is aligned with the left edge;
141 * otherwise, it is aligned with the right edge. This is the same
142 * behavior as with {@link #LEADING}.
144 * @see #getTitleJustification()
145 * @see #setTitleJustification(int)
146 * @see java.awt.ComponentOrientation#isLeftToRight()
148 public static final int DEFAULT_JUSTIFICATION = 0;
152 * A value for the <code>titleJustification</code> property that
153 * horizontally aligns the title text with the left-hand edge of
154 * the border.
156 * @see #getTitleJustification()
157 * @see #setTitleJustification(int)
159 public static final int LEFT = 1;
163 * A value for the <code>titleJustification</code> property that
164 * horizontally aligns the title text with the center of the border.
166 * @see #getTitleJustification()
167 * @see #setTitleJustification(int)
169 public static final int CENTER = 2;
173 * A value for the <code>titleJustification</code> property that
174 * horizontally aligns the title text with the right-hand edge of
175 * the border.
177 * @see #getTitleJustification()
178 * @see #setTitleJustification(int)
180 public static final int RIGHT = 3;
184 * A value for the <code>titleJustification</code> property that
185 * horizontally aligns the title text with either the left or the
186 * right edge of the border, depending on the orientation of the
187 * component nested into the border. If the component orientation
188 * is left-to-right, the title text is aligned with the left edge;
189 * otherwise, it is aligned with the right edge. This is the same
190 * behavior as with {@link #DEFAULT_JUSTIFICATION}.
192 * @see #getTitleJustification()
193 * @see #setTitleJustification(int)
194 * @see java.awt.ComponentOrientation#isLeftToRight()
196 public static final int LEADING = 4;
200 * A value for the <code>titleJustification</code> property that
201 * horizontally aligns the title text with either the right or the
202 * left edge of the border, depending on the orientation of the
203 * component nested into the border. If the component orientation
204 * is left-to-right, the title text is aligned with the right edge;
205 * otherwise, it is aligned with the left edge.
207 * @see #getTitleJustification()
208 * @see #setTitleJustification(int)
209 * @see java.awt.ComponentOrientation#isLeftToRight()
211 public static final int TRAILING = 5;
215 * The number of pixels between the inside of {@link #border}
216 * and the bordered component.
218 protected static final int EDGE_SPACING = 2;
222 * The number of pixels between the outside of this TitledBorder
223 * and the beginning (if left-aligned) or end (if right-aligned)
224 * of the title text.
226 protected static final int TEXT_INSET_H = 5;
230 * The number of pixels between the title text and {@link #border}.
231 * This value is only relevant if the title text does not intersect
232 * {@link #border}. No intersection occurs if {@link #titlePosition}
233 * is one of {@link #ABOVE_TOP}, {@link #BELOW_TOP}, {@link #ABOVE_BOTTOM},
234 * or {@link #BELOW_BOTTOM}.
236 protected static final int TEXT_SPACING = 2;
240 * Determined using the <code>serialver</code> tool of Apple/Sun JDK 1.3.1
241 * on MacOS X 10.1.5.
243 static final long serialVersionUID = 8012999415147721601L;
247 * The title, or <code>null</code> to display no title.
249 protected String title;
253 * The border underneath the title. If this value is
254 * <code>null</code>, the border will be retrieved from the {@link
255 * javax.swing.UIManager}&#x2019;s defaults table using the key
256 * <code>TitledBorder.border</code>.
258 protected Border border;
262 * The vertical position of the title text relative to the border,
263 * which is one of {@link #ABOVE_TOP}, {@link #TOP}, {@link
264 * #BELOW_TOP}, {@link #ABOVE_BOTTOM}, {@link #BOTTOM}, {@link
265 * #BELOW_BOTTOM}, or {@link #DEFAULT_POSITION}.
267 protected int titlePosition;
271 * The horizontal alignment of the title text in relation to the
272 * border, which is one of {@link #LEFT}, {@link #CENTER}, {@link
273 * #RIGHT}, {@link #LEADING}, {@link #TRAILING}, or {@link
274 * #DEFAULT_JUSTIFICATION}.
276 protected int titleJustification;
280 * The font for displaying the title text. If this value is
281 * <code>null</code>, the font will be retrieved from the {@link
282 * javax.swing.UIManager}&#x2019;s defaults table using the key
283 * <code>TitledBorder.font</code>.
285 protected Font titleFont;
289 * The color for displaying the title text. If this value is
290 * <code>null</code>, the color will be retrieved from the {@link
291 * javax.swing.UIManager}&#x2019;s defaults table using the key
292 * <code>TitledBorder.titleColor</code>.
294 protected Color titleColor;
298 * Constructs a TitledBorder given the text of its title.
300 * @param title the title text, or <code>null</code> to use no title text.
302 public TitledBorder(String title)
304 this(/* border */ null,
305 title, LEADING, TOP,
306 /* titleFont */ null, /* titleColor */ null);
311 * Constructs an initially untitled TitledBorder given another border.
313 * @param border the border underneath the title, or <code>null</code>
314 * to use a default from the current look and feel.
316 public TitledBorder(Border border)
318 this(border, /* title */ "", LEADING, TOP,
319 /* titleFont */ null, /* titleColor */ null);
324 * Constructs a TitledBorder given its border and title text.
326 * @param border the border underneath the title, or <code>null</code>
327 * to use a default from the current look and feel.
329 * @param title the title text, or <code>null</code> to use no title
330 * text.
332 public TitledBorder(Border border, String title)
334 this(border, title, LEADING, TOP,
335 /* titleFont */ null, /* titleColor */ null);
340 * Constructs a TitledBorder given its border, title text, horizontal
341 * alignment, and vertical position.
343 * @param border the border underneath the title, or <code>null</code>
344 * to use a default from the current look and feel.
346 * @param title the title text, or <code>null</code> to use no title
347 * text.
349 * @param titleJustification the horizontal alignment of the title
350 * text in relation to the border. The value must be one of
351 * {@link #LEFT}, {@link #CENTER}, {@link #RIGHT}, {@link #LEADING},
352 * {@link #TRAILING}, or {@link #DEFAULT_JUSTIFICATION}.
354 * @param titlePosition the vertical position of the title text
355 * in relation to the border. The value must be one of
356 * {@link #ABOVE_TOP}, {@link #TOP}, {@link #BELOW_TOP},
357 * {@link #ABOVE_BOTTOM}, {@link #BOTTOM}, {@link #BELOW_BOTTOM},
358 * or {@link #DEFAULT_POSITION}.
360 * @throws IllegalArgumentException if <code>titleJustification</code>
361 * or <code>titlePosition</code> have an unsupported value.
363 public TitledBorder(Border border, String title, int titleJustification,
364 int titlePosition)
366 this(border, title, titleJustification, titlePosition,
367 /* titleFont */ null, /* titleColor */ null);
372 * Constructs a TitledBorder given its border, title text, horizontal
373 * alignment, vertical position, and font.
375 * @param border the border underneath the title, or <code>null</code>
376 * to use a default from the current look and feel.
378 * @param title the title text, or <code>null</code> to use no title
379 * text.
381 * @param titleJustification the horizontal alignment of the title
382 * text in relation to the border. The value must be one of
383 * {@link #LEFT}, {@link #CENTER}, {@link #RIGHT}, {@link #LEADING},
384 * {@link #TRAILING}, or {@link #DEFAULT_JUSTIFICATION}.
386 * @param titlePosition the vertical position of the title text
387 * in relation to the border. The value must be one of
388 * {@link #ABOVE_TOP}, {@link #TOP}, {@link #BELOW_TOP},
389 * {@link #ABOVE_BOTTOM}, {@link #BOTTOM}, {@link #BELOW_BOTTOM},
390 * or {@link #DEFAULT_POSITION}.
392 * @param titleFont the font for the title text, or <code>null</code>
393 * to use a default from the current look and feel.
395 * @throws IllegalArgumentException if <code>titleJustification</code>
396 * or <code>titlePosition</code> have an unsupported value.
398 public TitledBorder(Border border, String title, int titleJustification,
399 int titlePosition, Font titleFont)
401 this(border, title, titleJustification, titlePosition, titleFont,
402 /* titleColor */ null);
407 * Constructs a TitledBorder given its border, title text, horizontal
408 * alignment, vertical position, font, and color.
410 * @param border the border underneath the title, or <code>null</code>
411 * to use a default from the current look and feel.
413 * @param title the title text, or <code>null</code> to use no title
414 * text.
416 * @param titleJustification the horizontal alignment of the title
417 * text in relation to the border. The value must be one of
418 * {@link #LEFT}, {@link #CENTER}, {@link #RIGHT}, {@link #LEADING},
419 * {@link #TRAILING}, or {@link #DEFAULT_JUSTIFICATION}.
421 * @param titlePosition the vertical position of the title text
422 * in relation to the border. The value must be one of
423 * {@link #ABOVE_TOP}, {@link #TOP}, {@link #BELOW_TOP},
424 * {@link #ABOVE_BOTTOM}, {@link #BOTTOM}, {@link #BELOW_BOTTOM},
425 * or {@link #DEFAULT_POSITION}.
427 * @param titleFont the font for the title text, or <code>null</code>
428 * to use a default from the current look and feel.
430 * @param titleColor the color for the title text, or <code>null</code>
431 * to use a default from the current look and feel.
433 * @throws IllegalArgumentException if <code>titleJustification</code>
434 * or <code>titlePosition</code> have an unsupported value.
436 public TitledBorder(Border border, String title, int titleJustification,
437 int titlePosition, Font titleFont, Color titleColor)
439 this.border = border;
440 this.title = title;
442 /* Invoking the setter methods ensures that the newly constructed
443 * TitledBorder has valid property values.
445 setTitleJustification(titleJustification);
446 setTitlePosition(titlePosition);
448 this.titleFont = titleFont;
449 this.titleColor = titleColor;
454 * Paints the border and the title text.
456 * @param c the component whose border is to be painted.
457 * @param g the graphics for painting.
458 * @param x the horizontal position for painting the border.
459 * @param y the vertical position for painting the border.
460 * @param width the width of the available area for painting the border.
461 * @param height the height of the available area for painting the border.
463 public void paintBorder(Component c, Graphics g,
464 int x, int y, int width, int height)
466 Rectangle borderRect = new Rectangle(x + EDGE_SPACING, y + EDGE_SPACING,
467 width - (EDGE_SPACING * 2),
468 height - (EDGE_SPACING * 2));
469 Point textLoc = new Point();
471 // Save color and font.
472 Color savedColor = g.getColor();
473 Font savedFont = g.getFont();
475 // The font metrics.
476 Font font = getFont(c);
477 g.setFont(font);
478 FontMetrics fm = c.getFontMetrics(font);
480 layoutBorderWithTitle(c, fm, borderRect, textLoc);
481 paintBorderWithTitle(c, g, x, y, width, height, borderRect, textLoc, fm);
483 g.setColor(getTitleColor());
484 g.drawString(getTitle(), textLoc.x, textLoc.y);
485 g.setFont(savedFont);
486 g.setColor(savedColor);
490 * Calculates the bounding box of the inner border and the location of the
491 * title string.
493 * @param c the component on which to paint the border
494 * @param fm the font metrics
495 * @param borderRect output parameter, holds the bounding box of the inner
496 * border on method exit
497 * @param textLoc output parameter, holds the location of the title text
498 * on method exit
500 private void layoutBorderWithTitle(Component c, FontMetrics fm,
501 Rectangle borderRect,
502 Point textLoc)
504 Border b = getBorder();
506 // The font metrics.
507 int fontHeight = fm.getHeight();
508 int fontDescent = fm.getDescent();
509 int fontAscent = fm.getAscent();
510 int titleWidth = fm.stringWidth(getTitle());
512 // The base insets.
513 Insets insets;
514 if (b == null)
515 insets = new Insets(0, 0, 0, 0);
516 else
517 insets = b.getBorderInsets(c);
519 // The offset of the border rectangle, dependend on the title placement.
520 int offset;
522 // Layout border and text vertically.
523 int titlePosition = getTitlePosition();
524 switch (titlePosition)
526 case ABOVE_BOTTOM:
527 textLoc.y = borderRect.y + borderRect.height - insets.bottom
528 - fontDescent - TEXT_SPACING;
529 break;
530 case BOTTOM:
531 borderRect.height -= fontHeight / 2;
532 textLoc.y = borderRect.y + borderRect.height - fontDescent
533 + (fontAscent + fontDescent - insets.bottom) / 2;
534 break;
535 case BELOW_BOTTOM:
536 borderRect.height -= fontHeight;
537 textLoc.y = borderRect.y + borderRect.height + fontAscent
538 + TEXT_SPACING;
539 break;
540 case ABOVE_TOP:
541 offset = fontAscent + fontDescent
542 + Math.max(EDGE_SPACING, TEXT_SPACING * 2) - EDGE_SPACING;
543 borderRect.y += offset;
544 borderRect.height -= offset;
545 textLoc.y = borderRect.y - (fontDescent + TEXT_SPACING);
546 break;
547 case BELOW_TOP:
548 textLoc.y = borderRect.y + insets.top + fontAscent + TEXT_SPACING;
549 break;
550 case TOP:
551 case DEFAULT_POSITION:
552 default:
553 offset = Math.max(0, ((fontAscent / 2) + TEXT_SPACING) - EDGE_SPACING);
554 borderRect.y += offset;
555 borderRect.height -= offset;
556 textLoc.y = borderRect.y - fontDescent
557 + (insets.top + fontAscent + fontDescent) / 2;
558 break;
561 // Layout border and text horizontally.
562 int justification = getTitleJustification();
563 // Adjust justification for LEADING and TRAILING depending on the direction
564 // of the component.
565 if (c.getComponentOrientation().isLeftToRight())
567 if (justification == LEADING || justification == DEFAULT_JUSTIFICATION)
568 justification = LEFT;
569 else if (justification == TRAILING)
570 justification = RIGHT;
572 else
574 if (justification == LEADING || justification == DEFAULT_JUSTIFICATION)
575 justification = RIGHT;
576 else if (justification == TRAILING)
577 justification = LEFT;
580 switch (justification)
582 case CENTER:
583 textLoc.x = borderRect.x + (borderRect.width - titleWidth) / 2;
584 break;
585 case RIGHT:
586 textLoc.x = borderRect.x + borderRect.width - titleWidth
587 - TEXT_INSET_H - insets.right;
588 break;
589 case LEFT:
590 default:
591 textLoc.x = borderRect.x + TEXT_INSET_H + insets.left;
596 * Paints the border with the title.
598 * @param c the component to paint on
599 * @param g the graphics context used for paintin
600 * @param x the upper left corner of the whole border
601 * @param y the upper left corner of the whole border
602 * @param width the width of the whole border
603 * @param height the width of the whole border
604 * @param borderRect the bounding box of the inner border
605 * @param textLoc the location of the border title
606 * @param fm the font metrics of the title
608 private void paintBorderWithTitle(Component c, Graphics g, int x, int y,
609 int width, int height,
610 Rectangle borderRect, Point textLoc,
611 FontMetrics fm)
613 Border b = getBorder();
614 int fontDescent = fm.getDescent();
615 int fontAscent = fm.getAscent();
616 int titleWidth = fm.stringWidth(getTitle());
618 if (b != null)
620 // Paint border in segments, when the title is painted above the
621 // border.
622 if (((titlePosition == TOP || titlePosition == DEFAULT_POSITION)
623 && (borderRect.y > textLoc.y - fontAscent))
624 || (titlePosition == BOTTOM
625 && borderRect.y + borderRect.height < textLoc.y + fontDescent))
627 Rectangle clip = new Rectangle();
628 Rectangle saved = g.getClipBounds();
630 // Paint border left from the text.
631 clip.setBounds(saved);
632 SwingUtilities.computeIntersection(x, y, textLoc.x - x - 1,
633 height, clip);
634 if (! clip.isEmpty())
636 g.setClip(clip);
637 b.paintBorder(c, g, borderRect.x, borderRect.y,
638 borderRect.width,
639 borderRect.height);
641 // Paint border right from the text.
642 clip.setBounds(saved);
643 SwingUtilities.computeIntersection(textLoc.x + titleWidth + 1, y,
644 x + width - (textLoc.x + titleWidth + 1), height, clip);
645 if (! clip.isEmpty())
647 g.setClip(clip);
648 b.paintBorder(c, g, borderRect.x, borderRect.y,
649 borderRect.width,
650 borderRect.height);
653 if (titlePosition == TOP || titlePosition == DEFAULT_POSITION)
655 // Paint border below the text.
656 clip.setBounds(saved);
657 SwingUtilities.computeIntersection(textLoc.x - 1,
658 textLoc.y + fontDescent,
659 titleWidth + 2,
660 y + height - textLoc.y - fontDescent,
661 clip);
662 if (! clip.isEmpty())
664 g.setClip(clip);
665 b.paintBorder(c, g, borderRect.x, borderRect.y,
666 borderRect.width,
667 borderRect.height);
671 else
673 // Paint border above the text.
674 clip.setBounds(saved);
675 SwingUtilities.computeIntersection(textLoc.x - 1, y,
676 titleWidth + 2,
677 textLoc.y - fontDescent - y,
678 clip);
679 if (! clip.isEmpty())
681 g.setClip(clip);
682 b.paintBorder(c, g, borderRect.x, borderRect.y,
683 borderRect.width,
684 borderRect.height);
688 g.setClip(saved);
690 else
692 b.paintBorder(c, g, borderRect.x, borderRect.y, borderRect.width,
693 borderRect.height);
699 * Measures the width of this border.
701 * @param c the component whose border is to be measured.
703 * @return an Insets object whose <code>left</code>, <code>right</code>,
704 * <code>top</code> and <code>bottom</code> fields indicate the
705 * width of the border at the respective edge.
707 * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
709 public Insets getBorderInsets(Component c)
711 return getBorderInsets(c, new Insets(0, 0, 0, 0));
716 * Measures the width of this border, storing the results into a
717 * pre-existing Insets object.
719 * @param insets an Insets object for holding the result values.
720 * After invoking this method, the <code>left</code>,
721 * <code>right</code>, <code>top</code> and
722 * <code>bottom</code> fields indicate the width of the
723 * border at the respective edge.
725 * @return the same object that was passed for <code>insets</code>.
727 * @see #getBorderInsets(Component)
729 public Insets getBorderInsets(Component c, Insets insets)
731 // Initialize insets with the insets from our border.
732 Border border = getBorder();
733 if (border != null)
735 if (border instanceof AbstractBorder)
737 AbstractBorder aBorder = (AbstractBorder) border;
738 aBorder.getBorderInsets(c, insets);
740 else
742 Insets i = border.getBorderInsets(c);
743 insets.top = i.top;
744 insets.bottom = i.bottom;
745 insets.left = i.left;
746 insets.right = i.right;
749 else
751 insets.top = 0;
752 insets.bottom = 0;
753 insets.left = 0;
754 insets.right = 0;
757 // Add spacing.
758 insets.top += EDGE_SPACING + TEXT_SPACING;
759 insets.bottom += EDGE_SPACING + TEXT_SPACING;
760 insets.left += EDGE_SPACING + TEXT_SPACING;
761 insets.right += EDGE_SPACING + TEXT_SPACING;
763 String title = getTitle();
764 if (c != null && title != null && !title.equals(""))
766 Font font = getFont(c);
767 FontMetrics fm = c.getFontMetrics(font);
768 int ascent = fm.getAscent();
769 int descent = fm.getDescent();
770 int height = fm.getHeight();
771 switch (getTitlePosition())
773 case ABOVE_BOTTOM:
774 insets.bottom += ascent + descent + TEXT_SPACING;
775 break;
776 case BOTTOM:
777 insets.bottom += ascent + descent;
778 break;
779 case BELOW_BOTTOM:
780 insets.bottom += height;
781 break;
782 case ABOVE_TOP:
783 insets.top += ascent + descent +
784 Math.max(EDGE_SPACING, TEXT_SPACING * 2)
785 - EDGE_SPACING;
786 break;
787 case BELOW_TOP:
788 insets.top += ascent + descent + TEXT_SPACING;
789 break;
790 case TOP:
791 case DEFAULT_POSITION:
792 default:
793 insets.top += ascent + descent;
796 return insets;
801 * Returns <code>false</code>, indicating that there are pixels inside
802 * the area of this border where the background shines through.
804 * @return <code>false</code>.
806 public boolean isBorderOpaque()
808 /* Note that the AbstractBorder.isBorderOpaque would also return
809 * false, so there is actually no need to override the inherited
810 * implementation. However, GNU Classpath strives for exact
811 * compatibility with the Sun reference implementation, which
812 * overrides isBorderOpaque for unknown reasons.
814 return false;
819 * Returns the text of the title.
821 * @return the title text, or <code>null</code> if no title is
822 * displayed.
824 public String getTitle()
826 return title;
831 * Retrieves the border underneath the title. If no border has been
832 * set, or if it has been set to<code>null</code>, the current
833 * {@link javax.swing.LookAndFeel} will be asked for a border
834 * using the key <code>TitledBorder.border</code>.
836 * @return a border, or <code>null</code> if the current LookAndFeel
837 * does not provide a border for the key
838 * <code>TitledBorder.border</code>.
840 * @see javax.swing.UIManager#getBorder(Object)
842 public Border getBorder()
844 if (border != null)
845 return border;
847 return UIManager.getBorder("TitledBorder.border");
852 * Returns the vertical position of the title text in relation
853 * to the border.
855 * @return one of the values {@link #ABOVE_TOP}, {@link #TOP},
856 * {@link #BELOW_TOP}, {@link #ABOVE_BOTTOM}, {@link #BOTTOM},
857 * {@link #BELOW_BOTTOM}, or {@link #DEFAULT_POSITION}.
859 public int getTitlePosition()
861 return titlePosition;
866 * Returns the horizontal alignment of the title text in relation to
867 * the border.
869 * @return one of the values {@link #LEFT}, {@link #CENTER}, {@link
870 * #RIGHT}, {@link #LEADING}, {@link #TRAILING}, or {@link
871 * #DEFAULT_JUSTIFICATION}.
873 public int getTitleJustification()
875 return titleJustification;
880 * Retrieves the font for displaying the title text. If no font has
881 * been set, or if it has been set to<code>null</code>, the current
882 * {@link javax.swing.LookAndFeel} will be asked for a font
883 * using the key <code>TitledBorder.font</code>.
885 * @return a font, or <code>null</code> if the current LookAndFeel
886 * does not provide a font for the key
887 * <code>TitledBorder.font</code>.
889 * @see javax.swing.UIManager#getFont(Object)
891 public Font getTitleFont()
893 if (titleFont != null)
894 return titleFont;
896 return UIManager.getFont("TitledBorder.font");
901 * Retrieves the color for displaying the title text. If no color has
902 * been set, or if it has been set to<code>null</code>, the current
903 * {@link javax.swing.LookAndFeel} will be asked for a color
904 * using the key <code>TitledBorder.titleColor</code>.
906 * @return a color, or <code>null</code> if the current LookAndFeel
907 * does not provide a color for the key
908 * <code>TitledBorder.titleColor</code>.
910 * @see javax.swing.UIManager#getColor(Object)
912 public Color getTitleColor()
914 if (titleColor != null)
915 return titleColor;
917 return UIManager.getColor("TitledBorder.titleColor");
922 * Sets the text of the title.
924 * @param title the new title text, or <code>null</code> for displaying
925 * no text at all.
927 public void setTitle(String title)
929 // Swing borders are not JavaBeans, thus no need to fire an event.
930 this.title = title;
935 * Sets the border underneath the title.
937 * @param border a border, or <code>null</code> to use the
938 * border that is supplied by the current LookAndFeel.
940 * @see #getBorder()
942 public void setBorder(Border border)
944 // Swing borders are not JavaBeans, thus no need to fire an event.
945 this.border = border;
950 * Sets the vertical position of the title text in relation
951 * to the border.
953 * @param titlePosition one of the values {@link #ABOVE_TOP},
954 * {@link #TOP}, {@link #BELOW_TOP}, {@link #ABOVE_BOTTOM},
955 * {@link #BOTTOM}, {@link #BELOW_BOTTOM},
956 * or {@link #DEFAULT_POSITION}.
958 * @throws IllegalArgumentException if an unsupported value is passed
959 * for <code>titlePosition</code>.
961 public void setTitlePosition(int titlePosition)
963 if ((titlePosition < DEFAULT_POSITION) || (titlePosition > BELOW_BOTTOM))
964 throw new IllegalArgumentException(titlePosition
965 + " is not a valid title position.");
967 // Swing borders are not JavaBeans, thus no need to fire an event.
968 this.titlePosition = titlePosition;
973 * Sets the horizontal alignment of the title text in relation to the border.
975 * @param titleJustification the new alignment, which must be one of
976 * {@link #LEFT}, {@link #CENTER}, {@link #RIGHT}, {@link #LEADING},
977 * {@link #TRAILING}, or {@link #DEFAULT_JUSTIFICATION}.
979 * @throws IllegalArgumentException if an unsupported value is passed
980 * for <code>titleJustification</code>.
982 public void setTitleJustification(int titleJustification)
984 if ((titleJustification < DEFAULT_JUSTIFICATION)
985 || (titleJustification > TRAILING))
986 throw new IllegalArgumentException(titleJustification
987 + " is not a valid title justification.");
989 // Swing borders are not JavaBeans, thus no need to fire an event.
990 this.titleJustification = titleJustification;
995 * Sets the font for displaying the title text.
997 * @param titleFont the font, or <code>null</code> to use the font
998 * provided by the current {@link javax.swing.LookAndFeel}.
1000 * @see #getTitleFont()
1002 public void setTitleFont(Font titleFont)
1004 // Swing borders are not JavaBeans, thus no need to fire an event.
1005 this.titleFont = titleFont;
1010 * Sets the color for displaying the title text.
1012 * @param titleColor the color, or <code>null</code> to use the color
1013 * provided by the current {@link javax.swing.LookAndFeel}.
1015 * @see #getTitleColor()
1017 public void setTitleColor(Color titleColor)
1019 // Swing borders are not JavaBeans, thus no need to fire an event.
1020 this.titleColor = titleColor;
1025 * Calculates the minimum size needed for displaying the border
1026 * and its title.
1028 * @param c the Component for which this TitledBorder constitutes
1029 * a border.
1031 * @return The minimum size.
1033 public Dimension getMinimumSize(Component c)
1035 Insets i = getBorderInsets(c);
1036 Dimension minSize = new Dimension(i.left + i.right, i.top + i.bottom);
1037 Font font = getFont(c);
1038 FontMetrics fm = c.getFontMetrics(font);
1039 int titleWidth = fm.stringWidth(getTitle());
1040 switch (getTitlePosition())
1042 case ABOVE_TOP:
1043 case BELOW_BOTTOM:
1044 minSize.width = Math.max(minSize.width, titleWidth);
1045 break;
1046 case BELOW_TOP:
1047 case ABOVE_BOTTOM:
1048 case TOP:
1049 case BOTTOM:
1050 case DEFAULT_POSITION:
1051 default:
1052 minSize.width += titleWidth;
1054 return minSize;
1059 * Returns the font that is used for displaying the title text for
1060 * a given Component.
1062 * @param c the Component for which this TitledBorder is the border.
1064 * @return The font returned by {@link #getTitleFont()}, or a fallback
1065 * if {@link #getTitleFont()} returned <code>null</code>.
1067 protected Font getFont(Component c)
1069 Font f;
1071 f = getTitleFont();
1072 if (f != null)
1073 return f;
1075 return new Font("Dialog", Font.PLAIN, 12);