* javax/swing/JToggleButton.java (ToggleButtonModel):
[official-gcc.git] / libjava / javax / swing / JLabel.java
blobc6bc1bd3ca8dc1d1295cfbf6f0c9bb137e47ad1b
1 /* JLabel.java --
2 Copyright (C) 2002, 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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. */
38 package javax.swing;
40 import java.awt.Component;
41 import java.awt.Image;
42 import java.awt.Font;
43 import javax.accessibility.Accessible;
44 import javax.accessibility.AccessibleContext;
45 import javax.swing.Icon;
46 import javax.swing.plaf.LabelUI;
49 /**
50 * <p>
51 * A swing widget that displays a text message and/or an icon.
52 * </p>
54 public class JLabel extends JComponent implements Accessible, SwingConstants
56 /** DOCUMENT ME! */
57 private static final long serialVersionUID = 5496508283662221534L;
59 /**
60 * The Component the label will give focus to when its mnemonic is
61 * activated.
63 protected Component labelFor;
65 /** The label's text. */
66 private transient String labelText;
68 /** Where the label will be positioned horizontally. */
69 private transient int horizontalAlignment = CENTER;
71 /** Where the label text will be placed horizontally relative to the icon. */
72 private transient int horizontalTextPosition = TRAILING;
74 /** Where the label will be positioned vertically. */
75 private transient int verticalAlignment = CENTER;
77 /** Where the label text will be place vertically relative to the icon. */
78 private transient int verticalTextPosition = CENTER;
80 /** The icon painted when the label is enabled. */
81 private transient Icon activeIcon;
83 /** The icon painted when the label is disabled. */
84 private transient Icon disabledIcon;
86 /** The label's mnemnonic key. */
87 private transient char mnemonicKey;
89 /** The index of the menemonic character in the text. */
90 private transient int underlinedChar = -1;
92 /** The gap between the icon and the text. */
93 private transient int iconTextGap = 4;
95 /**
96 * Fired in a PropertyChangeEvent when the "disabledIcon" property changes.
98 public static final String DISABLED_ICON_CHANGED_PROPERTY = "disabledIcon";
101 * Fired in a PropertyChangeEvent when the "displayedMnemonic" property
102 * changes.
104 public static final String DISPLAYED_MNEMONIC_CHANGED_PROPERTY = "displayedMnemonic";
107 * Fired in a PropertyChangeEvent when the "displayedMnemonicIndex"
108 * property changes. */
109 public static final String DISPLAYED_MNEMONIC_INDEX_CHANGED_PROPERTY = "displayedMnemonicIndex";
112 * Fired in a PropertyChangeEvent when the "horizontalAlignment" property
113 * changes.
115 public static final String HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY = "horizontalAlignment";
118 * Fired in a PropertyChangeEvent when the "horizontalTextPosition" property
119 * changes.
121 public static final String HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY = "horizontalTextPosition";
123 /** Fired in a PropertyChangeEvent when the "icon" property changes. */
124 public static final String ICON_CHANGED_PROPERTY = "icon";
126 /** Fired in a PropertyChangeEvent when the "iconTextGap" property changes. */
127 public static final String ICON_TEXT_GAP_CHANGED_PROPERTY = "iconTextGap";
129 /** Fired in a PropertyChangeEvent when the "labelFor" property changes. */
130 public static final String LABEL_FOR_CHANGED_PROPERTY = "labelFor";
132 /** Fired in a PropertyChangeEvent when the "text" property changes. */
133 public static final String TEXT_CHANGED_PROPERTY = "text";
136 * Fired in a PropertyChangeEvent when the "verticalAlignment" property
137 * changes.
139 public static final String VERTICAL_ALIGNMENT_CHANGED_PROPERTY = "verticalAlignment";
142 * Fired in a PropertyChangeEvent when the "verticalTextPosition" property
143 * changes.
145 public static final String VERTICAL_TEXT_POSITION_CHANGED_PROPERTY = "verticalTextPosition";
148 * Creates a new horizontally and vertically centered JLabel object with no text and no
149 * icon.
151 public JLabel()
153 this(null, null, CENTER);
157 * Creates a new horizontally and vertically centered JLabel object with no text and the
158 * given icon.
160 * @param image The icon to use with the label.
162 public JLabel(Icon image)
164 this(null, image, CENTER);
168 * Creates a new vertically centered JLabel object with no text and the given icon and
169 * horizontal alignment. By default, the text is TRAILING the image.
171 * @param image The icon to use with the label.
172 * @param horizontalAlignment The horizontal alignment of the label.
174 public JLabel(Icon image, int horizontalAlignment)
176 this(null, image, horizontalAlignment);
180 * Creates a new horizontally and vertically centered JLabel object with no icon and the
181 * given text.
183 * @param text The text to use with the label.
185 public JLabel(String text)
187 this(text, null, CENTER);
191 * Creates a new vertically centered JLabel object with no icon and the given text and
192 * horizontal alignment.
194 * @param text The text to use with the label.
195 * @param horizontalAlignment The horizontal alignment of the label.
197 public JLabel(String text, int horizontalAlignment)
199 this(text, null, horizontalAlignment);
203 * Creates a new vertically centered JLabel object with the given text, icon, and horizontal
204 * alignment.
206 * @param text The text to use with the label.
207 * @param icon The icon to use with the label.
208 * @param horizontalAlignment The horizontal alignment of the label.
210 public JLabel(String text, Icon icon, int horizontalAlignment)
212 labelText = text;
213 activeIcon = icon;
214 this.horizontalAlignment = horizontalAlignment;
215 updateUI();
219 * This method returns the label's UI delegate.
221 * @return The label's UI delegate.
223 public LabelUI getUI()
225 return (LabelUI) ui;
229 * This method sets the label's UI delegate.
231 * @param ui The label's UI delegate.
233 public void setUI(LabelUI ui)
235 super.setUI(ui);
239 * This method resets the label's UI delegate to the default UI for the
240 * current look and feel.
242 public void updateUI()
244 setUI((LabelUI) UIManager.getUI(this));
248 * This method returns a name to identify which look and feel class will be
249 * the UI delegate for this label.
251 * @return The UIClass identifier. "LabelUI"
253 public String getUIClassID()
255 return "LabelUI";
259 * This method is used primarily for debugging purposes and returns a string
260 * that can be used to represent this label.
262 * @return A string to represent this label.
264 protected String paramString()
266 return "JLabel";
270 * This method returns the label text.
272 * @return The label text.
274 public String getText()
276 return labelText;
280 * This method changes the "text" property. The given text will be painted
281 * in the label.
283 * @param text The label's text.
285 public void setText(String text)
287 if (text != labelText)
289 String oldText = labelText;
290 labelText = text;
291 firePropertyChange(TEXT_CHANGED_PROPERTY, oldText, labelText);
292 if (labelText != null && labelText.length() <= underlinedChar)
293 setDisplayedMnemonicIndex(labelText.length() - 1);
298 * This method returns the active icon. The active icon is painted when the
299 * label is enabled.
301 * @return The active icon.
303 public Icon getIcon()
305 return activeIcon;
309 * This method changes the "icon" property. This icon (the active icon) will
310 * be the one displayed when the label is enabled.
312 * @param icon The active icon.
314 public void setIcon(Icon icon)
316 if (icon != activeIcon)
318 Icon oldIcon = activeIcon;
319 activeIcon = icon;
320 firePropertyChange(ICON_CHANGED_PROPERTY, oldIcon, activeIcon);
325 * This method returns the disabled icon. The disabled icon is painted when
326 * the label is disabled. If the disabled icon is null and the active icon is
327 * an ImageIcon, this method returns a grayed version of the icon. The grayed
328 * version of the icon becomes the disabledIcon.
330 * @return The disabled icon.
332 public Icon getDisabledIcon()
334 //FIXME: We should be gray-scaling the active icon and then returning it
335 if (disabledIcon == null && activeIcon instanceof ImageIcon)
336 setDisabledIcon(activeIcon);
337 return disabledIcon;
341 * This method changes the "disabledIcon" property. This icon (the disabled
342 * icon) will be the one displayed when the label is disabled.
344 * @param disabledIcon The disabled icon.
346 public void setDisabledIcon(Icon disabledIcon)
348 if (disabledIcon != this.disabledIcon)
350 Icon oldDisabledIcon = this.disabledIcon;
351 this.disabledIcon = disabledIcon;
352 firePropertyChange(DISABLED_ICON_CHANGED_PROPERTY, oldDisabledIcon,
353 this.disabledIcon);
358 * This method sets the keycode that will be the label's mnemonic. If the
359 * label is used as a label for another component, the label will give
360 * focus to that component when the mnemonic is activated.
362 * @param key The keycode to use for the mnemonic.
364 public void setDisplayedMnemonic(int key)
366 setDisplayedMnemonic((char) key);
370 * This method sets the character that will be the mnemonic used. If the
371 * label is used as a label for another component, the label will give
372 * focus to that component when the mnemonic is activated.
374 * @param aChar The character to use for the mnemonic.
376 public void setDisplayedMnemonic(char aChar)
378 if (aChar != mnemonicKey)
380 char oldKey = mnemonicKey;
381 mnemonicKey = aChar;
382 firePropertyChange(DISPLAYED_MNEMONIC_CHANGED_PROPERTY, oldKey,
383 mnemonicKey);
384 if (labelText != null)
385 setDisplayedMnemonicIndex(labelText.indexOf(mnemonicKey));
390 * This method returns the keycode that is used for the label's mnemonic.
392 * @return The keycode that is used for the label's mnemonic.
394 public int getDisplayedMnemonic()
396 return (int) mnemonicKey;
400 * This method sets which character in the text will be the underlined
401 * character. If the given index is -1, then this indicates that there is
402 * no mnemonic. If the index is less than -1 or if the index is equal to
403 * the length, this method will throw an IllegalArgumentException.
405 * @param index The index of the character to underline.
407 * @throws IllegalArgumentException If index less than -1 or index equals
408 * length.
410 public void setDisplayedMnemonicIndex(int index)
411 throws IllegalArgumentException
413 if (index < -1 || labelText != null && index >= labelText.length())
414 throw new IllegalArgumentException();
416 if (labelText == null || labelText.charAt(index) != mnemonicKey)
417 index = -1;
419 if (index != underlinedChar)
421 int oldIndex = underlinedChar;
422 underlinedChar = index;
423 firePropertyChange(DISPLAYED_MNEMONIC_INDEX_CHANGED_PROPERTY,
424 oldIndex, underlinedChar);
429 * This method returns which character in the text will be the underlined
430 * character.
432 * @return The index of the character that will be underlined.
434 public int getDisplayedMnemonicIndex()
436 return underlinedChar;
440 * This method ensures that the key is valid as a horizontal alignment.
441 * Valid keys are: LEFT, CENTER, RIGHT, LEADING, TRAILING
443 * @param key The key to check.
444 * @param message The message of the exception to be thrown if the key is
445 * invalid.
447 * @return The key if it's valid.
449 * @throws IllegalArgumentException If the key is invalid.
451 protected int checkHorizontalKey(int key, String message)
453 if (key != LEFT && key != CENTER && key != RIGHT && key != LEADING
454 && key != TRAILING)
455 throw new IllegalArgumentException(message);
456 else
457 return key;
461 * This method ensures that the key is valid as a vertical alignment. Valid
462 * keys are: TOP, CENTER, and BOTTOM.
464 * @param key The key to check.
465 * @param message The message of the exception to be thrown if the key is
466 * invalid.
468 * @return The key if it's valid.
470 * @throws IllegalArgumentException If the key is invalid.
472 protected int checkVerticalKey(int key, String message)
474 if (key != TOP && key != BOTTOM && key != CENTER)
475 throw new IllegalArgumentException(message);
476 else
477 return key;
481 * This method returns the gap between the icon and the text.
483 * @return The gap between the icon and the text.
485 public int getIconTextGap()
487 return iconTextGap;
491 * This method changes the "iconTextGap" property. The iconTextGap
492 * determines how much space there is between the icon and the text.
494 * @param iconTextGap The gap between the icon and the text.
496 public void setIconTextGap(int iconTextGap)
498 if (iconTextGap != this.iconTextGap)
500 int oldIconTextGap = this.iconTextGap;
501 this.iconTextGap = iconTextGap;
502 firePropertyChange(ICON_TEXT_GAP_CHANGED_PROPERTY, oldIconTextGap,
503 iconTextGap);
508 * This method returns the vertical alignment of the label.
510 * @return The vertical alignment of the label.
512 public int getVerticalAlignment()
514 return verticalAlignment;
518 * This method changes the "verticalAlignment" property of the label. The
519 * vertical alignment determines how where the label will be placed
520 * vertically. If the alignment is not valid, it will default to the
521 * center.
523 * @param alignment The vertical alignment of the label.
525 public void setVerticalAlignment(int alignment)
527 if (alignment != verticalAlignment)
529 int oldAlignment = verticalAlignment;
530 verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
531 firePropertyChange(VERTICAL_ALIGNMENT_CHANGED_PROPERTY, oldAlignment,
532 verticalAlignment);
537 * This method returns the horziontal alignment of the label.
539 * @return The horizontal alignment of the label.
541 public int getHorizontalAlignment()
543 return horizontalAlignment;
547 * This method changes the "horizontalAlignment" property. The horizontal
548 * alignment determines where the label will be placed horizontally.
550 * @param alignment The horizontal alignment of the label.
552 public void setHorizontalAlignment(int alignment)
554 int oldAlignment = horizontalAlignment;
555 horizontalAlignment = checkHorizontalKey(alignment, "horizontalAlignment");
556 firePropertyChange(HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY, oldAlignment,
557 horizontalAlignment);
561 * This method returns the vertical text position of the label.
563 * @return The vertical text position of the label.
565 public int getVerticalTextPosition()
567 return verticalTextPosition;
571 * This method changes the "verticalTextPosition" property of the label. The
572 * vertical text position determines where the text will be placed
573 * vertically relative to the icon.
575 * @param textPosition The vertical text position.
577 public void setVerticalTextPosition(int textPosition)
579 if (textPosition != verticalTextPosition)
581 int oldPos = verticalTextPosition;
582 verticalTextPosition = checkVerticalKey(textPosition,
583 "verticalTextPosition");
584 firePropertyChange(VERTICAL_TEXT_POSITION_CHANGED_PROPERTY, oldPos,
585 verticalTextPosition);
590 * This method returns the horizontal text position of the label.
592 * @return The horizontal text position.
594 public int getHorizontalTextPosition()
596 return horizontalTextPosition;
600 * This method changes the "horizontalTextPosition" property of the label.
601 * The horizontal text position determines where the text will be placed
602 * horizontally relative to the icon.
604 * @param textPosition The horizontal text position.
606 public void setHorizontalTextPosition(int textPosition)
608 if (textPosition != horizontalTextPosition)
610 int oldPos = horizontalTextPosition;
611 horizontalTextPosition = checkHorizontalKey(textPosition,
612 "horizontalTextPosition");
613 firePropertyChange(HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY, oldPos,
614 horizontalTextPosition);
619 * This method simply returns false if the current icon image (current icon
620 * will depend on whether the label is enabled) is not equal to the passed
621 * in image.
623 * @param img The image to check.
624 * @param infoflags The bitwise inclusive OR of ABORT, ALLBITS, ERROR,
625 * FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, and WIDTH
626 * @param x The x position
627 * @param y The y position
628 * @param w The width
629 * @param h The height
631 * @return Whether the current icon image is equal to the image given.
633 public boolean imageUpdate(Image img, int infoflags, int x, int y, int w,
634 int h)
636 Icon currIcon = (isEnabled()) ? activeIcon : disabledIcon;
638 //Is this the correct way to check for image equality?
639 if (currIcon != null && currIcon instanceof ImageIcon)
640 return (((ImageIcon) currIcon).getImage() == img);
641 return false;
645 * This method returns the component that the label gives focus to when the
646 * mnemonic is activated.
648 * @return The component that gets focus when the label's mnemonic is
649 * activated.
651 public Component getLabelFor()
653 return labelFor;
657 * This method changes the "labelFor" property. The component that the label
658 * is acting as a label for will request focus when the label's mnemonic
659 * is activated.
661 * @param c The component that gets focus when the label's mnemonic is
662 * activated.
664 public void setLabelFor(Component c)
666 if (c != labelFor)
668 Component oldLabelFor = labelFor;
669 labelFor = c;
670 firePropertyChange(LABEL_FOR_CHANGED_PROPERTY, oldLabelFor, labelFor);
675 * This method overrides setFont so that we can call for a repaint
676 * after the font is changed.
678 * @param f The font for this label.
680 public void setFont(Font f)
682 super.setFont(f);
683 repaint();
687 * DOCUMENT ME!
689 * @return
691 public AccessibleContext getAccessibleContext()
693 return null;