1. /*
  2. * @(#)JLabel.java 1.90 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.Component;
  9. import java.awt.Font;
  10. import java.awt.Image;
  11. import java.io.ObjectOutputStream;
  12. import java.io.ObjectInputStream;
  13. import java.io.IOException;
  14. import javax.swing.plaf.LabelUI;
  15. import javax.accessibility.*;
  16. /**
  17. * A display area for a short text string or an image,
  18. * or both.
  19. * A label does not react to input events.
  20. * As a result, it cannot get the keyboard focus.
  21. * A label can, however, display a keyboard alternative
  22. * as a convenience for a nearby component
  23. * that has a keyboard alternative but can't display it.
  24. * <p>
  25. * A <code>JLabel</code> object can display
  26. * either text, an image, or both.
  27. * You can specify where in the label's display area
  28. * the label's contents are aligned
  29. * by setting the vertical and horizontal alignment.
  30. * By default, labels are vertically centered
  31. * in their display area.
  32. * Text-only labels are leading edge aligned, by default;
  33. * image-only labels are horizontally centered, by default.
  34. * <p>
  35. * You can also specify the position of the text
  36. * relative to the image.
  37. * By default, text is on the trailing edge of the image,
  38. * with the text and image vertically aligned.
  39. * <p>
  40. * A label's leading and trailing edge are determined from the value of its
  41. * {@link java.awt.ComponentOrientation} property. At present, the default
  42. * ComponentOrientation setting maps the leading edge to left and the trailing
  43. * edge to right.
  44. *
  45. * <p>
  46. * Finally, you can use the <code>setIconTextGap</code> method
  47. * to specify how many pixels
  48. * should appear between the text and the image.
  49. * The default is 4 pixels.
  50. * <p>
  51. * See <a href="http://java.sun.com/docs/books/tutorial/ui/swing/label.html">How to Use Labels</a>
  52. * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
  53. * for further documentation.
  54. * <p>
  55. * <strong>Warning:</strong>
  56. * Serialized objects of this class will not be compatible with
  57. * future Swing releases. The current serialization support is appropriate
  58. * for short term storage or RMI between applications running the same
  59. * version of Swing. A future release of Swing will provide support for
  60. * long term persistence.
  61. *
  62. * @beaninfo
  63. * attribute: isContainer false
  64. * description: A component that displays a short string and an icon.
  65. *
  66. * @version 1.90 11/29/01
  67. * @author Hans Muller
  68. */
  69. public class JLabel extends JComponent implements SwingConstants, Accessible
  70. {
  71. /**
  72. * @see #getUIClassID
  73. * @see #readObject
  74. */
  75. private static final String uiClassID = "LabelUI";
  76. private int mnemonic = '\0';
  77. private String text = ""; // "" rather than null, for BeanBox
  78. private Icon defaultIcon = null;
  79. private Icon disabledIcon = null;
  80. private boolean disabledIconSet = false;
  81. private int verticalAlignment = CENTER;
  82. private int horizontalAlignment = LEADING;
  83. private int verticalTextPosition = CENTER;
  84. private int horizontalTextPosition = TRAILING;
  85. private int iconTextGap = 4;
  86. protected Component labelFor = null;
  87. /**
  88. * Client property key used to determine what label is labeling the
  89. * component. This is generally not used by labels, but is instead
  90. * used by components such as text areas that are being labeled by
  91. * labels. When the labelFor property of a label is set, it will
  92. * automatically set the LABELED_BY_PROPERTY of the component being
  93. * labelled.
  94. *
  95. * @see #setLabelFor
  96. */
  97. static final String LABELED_BY_PROPERTY = "labeledBy";
  98. /**
  99. * Creates a <code>JLabel</code> instance with the specified
  100. * text, image, and horizontal alignment.
  101. * The label is centered vertically in its display area.
  102. * The text is on the trailing edge of the image.
  103. *
  104. * @param text The text to be displayed by the label.
  105. * @param icon The image to be displayed by the label.
  106. * @param horizontalAlignment One of the following constants
  107. * defined in <code>SwingConstants</code>:
  108. * <code>LEFT</code>,
  109. * <code>CENTER</code>,
  110. * <code>RIGHT</code>,
  111. * <code>LEADING</code> or
  112. * <code>TRAILING</code>.
  113. */
  114. public JLabel(String text, Icon icon, int horizontalAlignment) {
  115. setText(text);
  116. setIcon(icon);
  117. setHorizontalAlignment(horizontalAlignment);
  118. updateUI();
  119. setAlignmentX(LEFT_ALIGNMENT);
  120. }
  121. /**
  122. * Creates a <code>JLabel</code> instance with the specified
  123. * text and horizontal alignment.
  124. * The label is centered vertically in its display area.
  125. *
  126. * @param text The text to be displayed by the label.
  127. * @param horizontalAlignment One of the following constants
  128. * defined in <code>SwingConstants</code>:
  129. * <code>LEFT</code>,
  130. * <code>CENTER</code>,
  131. * <code>RIGHT</code>,
  132. * <code>LEADING</code> or
  133. * <code>TRAILING</code>.
  134. */
  135. public JLabel(String text, int horizontalAlignment) {
  136. this(text, null, horizontalAlignment);
  137. }
  138. /**
  139. * Creates a <code>JLabel</code> instance with the specified text.
  140. * The label is aligned against the leading edge of its display area,
  141. * and centered vertically.
  142. *
  143. * @param text The text to be displayed by the label.
  144. */
  145. public JLabel(String text) {
  146. this(text, null, LEADING);
  147. }
  148. /**
  149. * Creates a <code>JLabel</code> instance with the specified
  150. * image and horizontal alignment.
  151. * The label is centered vertically in its display area.
  152. *
  153. * @param icon The image to be displayed by the label.
  154. * @param horizontalAlignment One of the following constants
  155. * defined in <code>SwingConstants</code>:
  156. * <code>LEFT</code>,
  157. * <code>CENTER</code>,
  158. * <code>RIGHT</code>,
  159. * <code>LEADING</code> or
  160. * <code>TRAILING</code>.
  161. */
  162. public JLabel(Icon image, int horizontalAlignment) {
  163. this(null, image, horizontalAlignment);
  164. }
  165. /**
  166. * Creates a <code>JLabel</code> instance with the specified image.
  167. * The label is centered vertically and horizontally
  168. * in its display area.
  169. *
  170. * @param icon The image to be displayed by the label.
  171. */
  172. public JLabel(Icon image) {
  173. this(null, image, CENTER);
  174. }
  175. /**
  176. * Creates a <code>JLabel</code> instance with
  177. * no image and with an empty string for the title.
  178. * The label is centered vertically
  179. * in its display area.
  180. * The label's contents, once set, will be displayed on the leading edge
  181. * of the label's display area.
  182. */
  183. public JLabel() {
  184. this("", null, LEADING);
  185. }
  186. /**
  187. * Returns the L&F object that renders this component.
  188. *
  189. * @return LabelUI object
  190. */
  191. public LabelUI getUI() {
  192. return (LabelUI)ui;
  193. }
  194. /**
  195. * Sets the L&F object that renders this component.
  196. *
  197. * @param ui the LabelUI L&F object
  198. * @see UIDefaults#getUI
  199. * @beaninfo
  200. * expert: true
  201. * description: The L&F object that renders this component.
  202. */
  203. public void setUI(LabelUI ui) {
  204. super.setUI(ui);
  205. }
  206. /**
  207. * Notification from the UIFactory that the L&F
  208. * has changed.
  209. *
  210. * @see JComponent#updateUI
  211. */
  212. public void updateUI() {
  213. setUI((LabelUI)UIManager.getUI(this));
  214. }
  215. /**
  216. * Returns a string that specifies the name of the l&f class
  217. * that renders this component.
  218. *
  219. * @return String "LabelUI"
  220. *
  221. * @see JComponent#getUIClassID
  222. * @see UIDefaults#getUI
  223. */
  224. public String getUIClassID() {
  225. return uiClassID;
  226. }
  227. /**
  228. * Returns the text string that the label displays.
  229. *
  230. * @return a String
  231. * @see #setText
  232. */
  233. public String getText() {
  234. return text;
  235. }
  236. /**
  237. * Defines the single line of text this component will display. If
  238. * the value of text is null or empty string, nothing is displayed.
  239. * <p>
  240. * The default value of this property is null.
  241. * <p>
  242. * This is a JavaBeans bound property.
  243. *
  244. * @see #setVerticalTextPosition
  245. * @see #setHorizontalTextPosition
  246. * @see #setIcon
  247. * @beaninfo
  248. * preferred: true
  249. * bound: true
  250. * attribute: visualUpdate true
  251. * description: Defines the single line of text this component will display.
  252. */
  253. public void setText(String text) {
  254. String oldAccessibleName = null;
  255. if (accessibleContext != null) {
  256. oldAccessibleName = accessibleContext.getAccessibleName();
  257. }
  258. String oldValue = this.text;
  259. this.text = text;
  260. firePropertyChange("text", oldValue, text);
  261. if ((accessibleContext != null)
  262. && (accessibleContext.getAccessibleName() != oldAccessibleName)) {
  263. accessibleContext.firePropertyChange(
  264. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  265. oldAccessibleName,
  266. accessibleContext.getAccessibleName());
  267. }
  268. if (text == null || oldValue == null || !text.equals(oldValue)) {
  269. revalidate();
  270. repaint();
  271. }
  272. }
  273. /**
  274. * Returns the graphic image (glyph, icon) that the label displays.
  275. *
  276. * @return an Icon
  277. * @see #setIcon
  278. */
  279. public Icon getIcon() {
  280. return defaultIcon;
  281. }
  282. /**
  283. * Defines the icon this component will display. If
  284. * the value of icon is null, nothing is displayed.
  285. * <p>
  286. * The default value of this property is null.
  287. * <p>
  288. * This is a JavaBeans bound property.
  289. *
  290. * @see #setVerticalTextPosition
  291. * @see #setHorizontalTextPosition
  292. * @see #getIcon
  293. * @beaninfo
  294. * preferred: true
  295. * bound: true
  296. * attribute: visualUpdate true
  297. * description: The icon this component will display.
  298. */
  299. public void setIcon(Icon icon) {
  300. Icon oldValue = defaultIcon;
  301. defaultIcon = icon;
  302. /* If the default icon has really changed and we had
  303. * generated the disabled icon for this component,
  304. * (i.e. setDiabledIcon() was never called) then
  305. * clear the disabledIcon field.
  306. */
  307. if ((defaultIcon != oldValue) && !disabledIconSet) {
  308. disabledIcon = null;
  309. }
  310. firePropertyChange("icon", oldValue, defaultIcon);
  311. if ((accessibleContext != null) && (oldValue != defaultIcon)) {
  312. accessibleContext.firePropertyChange(
  313. AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
  314. oldValue, defaultIcon);
  315. }
  316. /* If the default icon has changed and the new one is
  317. * a different size, then revalidate. Repaint if the
  318. * default icon has changed.
  319. */
  320. if (defaultIcon != oldValue) {
  321. if ((defaultIcon == null) ||
  322. (oldValue == null) ||
  323. (defaultIcon.getIconWidth() != oldValue.getIconWidth()) ||
  324. (defaultIcon.getIconHeight() != oldValue.getIconHeight())) {
  325. revalidate();
  326. }
  327. repaint();
  328. }
  329. }
  330. /**
  331. * Returns the value of the disabledIcon property if it's been set,
  332. * If it hasn't been set and the value of the icon property is
  333. * an ImageIcon, we compute a "grayed out" version of the icon and
  334. * update the disabledIcon property with that.
  335. *
  336. * @return The value of the disabledIcon property.
  337. * @see #setDisabledIcon
  338. * @see ImageIcon
  339. */
  340. public Icon getDisabledIcon()
  341. {
  342. if(!disabledIconSet &&
  343. (disabledIcon == null) &&
  344. (defaultIcon != null) &&
  345. (defaultIcon instanceof ImageIcon)) {
  346. Image grayImage = GrayFilter.createDisabledImage(((ImageIcon)defaultIcon).getImage());
  347. disabledIcon = new ImageIcon(grayImage);
  348. firePropertyChange("disabledIcon", null, disabledIcon);
  349. }
  350. return disabledIcon;
  351. }
  352. /**
  353. * Set the icon to be displayed if this JLabel is "disabled", i.e.
  354. * JLabel.setEnabled(false).
  355. * <p>
  356. * The default value of this property is null.
  357. *
  358. * @param disabledIcon the Icon to display when the component is disabled
  359. * @see #getDisabledIcon
  360. * @see #setEnabled
  361. * @beaninfo
  362. * bound: true
  363. * attribute: visualUpdate true
  364. * description: The icon to display if the label is disabled.
  365. */
  366. public void setDisabledIcon(Icon disabledIcon) {
  367. Icon oldValue = this.disabledIcon;
  368. this.disabledIcon = disabledIcon;
  369. disabledIconSet = true;
  370. firePropertyChange("disabledIcon", oldValue, disabledIcon);
  371. if (disabledIcon != oldValue) {
  372. if (disabledIcon == null || oldValue == null ||
  373. disabledIcon.getIconWidth() != oldValue.getIconWidth() ||
  374. disabledIcon.getIconHeight() != oldValue.getIconHeight()) {
  375. revalidate();
  376. }
  377. if (!isEnabled()) {
  378. repaint();
  379. }
  380. }
  381. }
  382. /**
  383. * Specify a keycode that indicates a mnemonic key.
  384. * This property is used when the label is part of a larger component.
  385. * If the labelFor property of the label is not null, the label will
  386. * call the requestFocus method of the component specified by the
  387. * labelFor property when the mnemonic is activated.
  388. *
  389. * @see #getLabelFor
  390. * @see #setLabelFor
  391. * @beaninfo
  392. * bound: true
  393. * attribute: visualUpdate true
  394. * description: The mnemonic keycode.
  395. */
  396. public void setDisplayedMnemonic(int key) {
  397. int oldKey = mnemonic;
  398. mnemonic = key;
  399. firePropertyChange("displayedMnemonic", oldKey, mnemonic);
  400. if (key != oldKey) {
  401. revalidate();
  402. repaint();
  403. }
  404. }
  405. /**
  406. * Specifies the displayedMnemonic as a char value.
  407. *
  408. * @param aChar a char specifying the mnemonic to display
  409. * @see #setDisplayedMnemonic(int)
  410. */
  411. public void setDisplayedMnemonic(char aChar) {
  412. int vk = (int) aChar;
  413. if(vk >= 'a' && vk <='z')
  414. vk -= ('a' - 'A');
  415. setDisplayedMnemonic(vk);
  416. }
  417. /**
  418. * Return the keycode that indicates a mnemonic key.
  419. * This property is used when the label is part of a larger component.
  420. * If the labelFor property of the label is not null, the label will
  421. * call the requestFocus method of the component specified by the
  422. * labelFor property when the mnemonic is activated.
  423. *
  424. * @return int value for the mnemonic key
  425. *
  426. * @see #getLabelFor
  427. * @see #setLabelFor
  428. */
  429. public int getDisplayedMnemonic() {
  430. return mnemonic;
  431. }
  432. /**
  433. * Verify that key is a legal value for the horizontalAlignment properties.
  434. *
  435. * @param key the property value to check
  436. * @param message the IllegalArgumentException detail message
  437. * @exception IllegalArgumentException if key isn't LEFT, CENTER, RIGHT,
  438. * LEADING or TRAILING.
  439. * @see #setHorizontalTextPosition
  440. * @see #setHorizontalAlignment
  441. */
  442. protected int checkHorizontalKey(int key, String message) {
  443. if ((key == LEFT) ||
  444. (key == CENTER) ||
  445. (key == RIGHT) ||
  446. (key == LEADING) ||
  447. (key == TRAILING)) {
  448. return key;
  449. }
  450. else {
  451. throw new IllegalArgumentException(message);
  452. }
  453. }
  454. /**
  455. * Verify that key is a legal value for the
  456. * verticalAlignment or verticalTextPosition properties.
  457. *
  458. * @param key the property value to check
  459. * @param message the IllegalArgumentException detail message
  460. * @exception IllegalArgumentException if key isn't TOP, CENTER, or BOTTOM.
  461. * @see #setVerticalAlignment
  462. * @see #setVerticalTextPosition
  463. */
  464. protected int checkVerticalKey(int key, String message) {
  465. if ((key == TOP) || (key == CENTER) || (key == BOTTOM)) {
  466. return key;
  467. }
  468. else {
  469. throw new IllegalArgumentException(message);
  470. }
  471. }
  472. /**
  473. * Returns the amount of space between the text and the icon
  474. * displayed in this label.
  475. *
  476. * @return an int equal to the number of pixels between the text
  477. * and the icon.
  478. * @see #setIconTextGap
  479. */
  480. public int getIconTextGap() {
  481. return iconTextGap;
  482. }
  483. /**
  484. * If both the icon and text properties are set, this property
  485. * defines the space between them.
  486. * <p>
  487. * The default value of this property is 4 pixels.
  488. * <p>
  489. * This is a JavaBeans bound property.
  490. *
  491. * @see #getIconTextGap
  492. * @beaninfo
  493. * bound: true
  494. * attribute: visualUpdate true
  495. * description: If both the icon and text properties are set, this
  496. * property defines the space between them.
  497. */
  498. public void setIconTextGap(int iconTextGap) {
  499. int oldValue = this.iconTextGap;
  500. this.iconTextGap = iconTextGap;
  501. firePropertyChange("iconTextGap", oldValue, iconTextGap);
  502. if (iconTextGap != oldValue) {
  503. revalidate();
  504. repaint();
  505. }
  506. }
  507. /**
  508. * Returns the alignment of the label's contents along the Y axis.
  509. *
  510. * @return The value of the verticalAlignment property, one of the
  511. * following constants defined in <code>SwingConstants</code>:
  512. * <code>TOP</code>,
  513. * <code>CENTER</code>, or
  514. * <code>BOTTOM</code>.
  515. *
  516. * @see SwingConstants
  517. * @see #setVerticalAlignment
  518. */
  519. public int getVerticalAlignment() {
  520. return verticalAlignment;
  521. }
  522. /**
  523. * Sets the alignment of the label's contents along the Y axis.
  524. * <p>
  525. * The default value of this property is CENTER.
  526. *
  527. * @param alignment One of the following constants
  528. * defined in <code>SwingConstants</code>:
  529. * <code>TOP</code>,
  530. * <code>CENTER</code> (the default), or
  531. * <code>BOTTOM</code>.
  532. *
  533. * @see SwingConstants
  534. * @see #getVerticalAlignment
  535. * @beaninfo
  536. * bound: true
  537. * enum: TOP SwingConstants.TOP
  538. * CENTER SwingConstants.CENTER
  539. * BOTTOM SwingConstants.BOTTOM
  540. * attribute: visualUpdate true
  541. * description: The alignment of the label's contents along the Y axis.
  542. */
  543. public void setVerticalAlignment(int alignment) {
  544. if (alignment == verticalAlignment) return;
  545. int oldValue = verticalAlignment;
  546. verticalAlignment = checkVerticalKey(alignment, "verticalAlignment");
  547. firePropertyChange("verticalAlignment", oldValue, verticalAlignment);
  548. repaint();
  549. }
  550. /**
  551. * Returns the alignment of the label's contents along the X axis.
  552. *
  553. * @return The value of the horizontalAlignment property, one of the
  554. * following constants defined in <code>SwingConstants</code>:
  555. * <code>LEFT</code>,
  556. * <code>CENTER</code>,
  557. * <code>RIGHT</code>,
  558. * <code>LEADING</code> or
  559. * <code>TRAILING</code>.
  560. *
  561. * @see #setHorizontalAlignment
  562. * @see SwingConstants
  563. */
  564. public int getHorizontalAlignment() {
  565. return horizontalAlignment;
  566. }
  567. /**
  568. * Sets the alignment of the label's contents along the X axis.
  569. * <p>
  570. * This is a JavaBeans bound property.
  571. *
  572. * @param alignment One of the following constants
  573. * defined in <code>SwingConstants</code>:
  574. * <code>LEFT</code>,
  575. * <code>CENTER</code> (the default for image-only labels),
  576. * <code>RIGHT</code>,
  577. * <code>LEADING</code> (the default for text-only labels) or
  578. * <code>TRAILING</code>.
  579. *
  580. * @see SwingConstants
  581. * @see #getHorizontalAlignment
  582. * @beaninfo
  583. * bound: true
  584. * enum: LEFT SwingConstants.LEFT
  585. * CENTER SwingConstants.CENTER
  586. * RIGHT SwingConstants.RIGHT
  587. * LEADING SwingConstants.LEADING
  588. * TRAILING SwingConstants.TRAILING
  589. * attribute: visualUpdate true
  590. * description: The alignment of the label's content along the X axis.
  591. */
  592. public void setHorizontalAlignment(int alignment) {
  593. if (alignment == horizontalAlignment) return;
  594. int oldValue = horizontalAlignment;
  595. horizontalAlignment = checkHorizontalKey(alignment,
  596. "horizontalAlignment");
  597. firePropertyChange("horizontalAlignment",
  598. oldValue, horizontalAlignment);
  599. repaint();
  600. }
  601. /**
  602. * Returns the vertical position of the label's text,
  603. * relative to its image.
  604. *
  605. * @return One of the following constants
  606. * defined in <code>SwingConstants</code>:
  607. * <code>TOP</code>,
  608. * <code>CENTER</code>, or
  609. * <code>BOTTOM</code>.
  610. *
  611. * @see #setVerticalTextPosition
  612. * @see SwingConstants
  613. */
  614. public int getVerticalTextPosition() {
  615. return verticalTextPosition;
  616. }
  617. /**
  618. * Sets the vertical position of the label's text,
  619. * relative to its image.
  620. * <p>
  621. * The default value of this property is CENTER.
  622. * <p>
  623. * This is a JavaBeans bound property.
  624. *
  625. * @param textPosition One of the following constants
  626. * defined in <code>SwingConstants</code>:
  627. * <code>TOP</code>,
  628. * <code>CENTER</code> (the default), or
  629. * <code>BOTTOM</code>.
  630. *
  631. * @see SwingConstants
  632. * @see #getVerticalTextPosition
  633. * @beaninfo
  634. * bound: true
  635. * enum: TOP SwingConstants.TOP
  636. * CENTER SwingConstants.CENTER
  637. * BOTTOM SwingConstants.BOTTOM
  638. * expert: true
  639. * attribute: visualUpdate true
  640. * description: The vertical position of the text relative to it's image.
  641. */
  642. public void setVerticalTextPosition(int textPosition) {
  643. if (textPosition == verticalTextPosition) return;
  644. int old = verticalTextPosition;
  645. verticalTextPosition = checkVerticalKey(textPosition,
  646. "verticalTextPosition");
  647. firePropertyChange("verticalTextPosition", old, verticalTextPosition);
  648. repaint();
  649. }
  650. /**
  651. * Returns the horizontal position of the label's text,
  652. * relative to its image.
  653. *
  654. * @return One of the following constants
  655. * defined in <code>SwingConstants</code>:
  656. * <code>LEFT</code>,
  657. * <code>CENTER</code>,
  658. * <code>RIGHT</code>,
  659. * <code>LEADING</code> or
  660. * <code>TRAILING</code>.
  661. *
  662. * @see SwingConstants
  663. */
  664. public int getHorizontalTextPosition() {
  665. return horizontalTextPosition;
  666. }
  667. /**
  668. * Sets the horizontal position of the label's text,
  669. * relative to its image.
  670. *
  671. * @param x One of the following constants
  672. * defined in <code>SwingConstants</code>:
  673. * <code>LEFT</code>,
  674. * <code>CENTER</code>,
  675. * <code>RIGHT</code>,
  676. * <code>LEADING</code>, or
  677. * <code>TRAILING</code> (the default).
  678. * @exception IllegalArgumentException
  679. *
  680. * @see SwingConstants
  681. * @beaninfo
  682. * expert: true
  683. * bound: true
  684. * enum: LEFT SwingConstants.LEFT
  685. * CENTER SwingConstants.CENTER
  686. * RIGHT SwingConstants.RIGHT
  687. * LEADING SwingConstants.LEADING
  688. * TRAILING SwingConstants.TRAILING
  689. * attribute: visualUpdate true
  690. * description: The horizontal position of the label's text,
  691. * relative to its image.
  692. */
  693. public void setHorizontalTextPosition(int textPosition) {
  694. int old = horizontalTextPosition;
  695. this.horizontalTextPosition = checkHorizontalKey(textPosition,
  696. "horizontalTextPosition");
  697. firePropertyChange("horizontalTextPosition",
  698. old, horizontalTextPosition);
  699. repaint();
  700. }
  701. /**
  702. * See readObject() and writeObject() in JComponent for more
  703. * information about serialization in Swing.
  704. */
  705. private void writeObject(ObjectOutputStream s) throws IOException {
  706. s.defaultWriteObject();
  707. if ((ui != null) && (getUIClassID().equals(uiClassID))) {
  708. ui.installUI(this);
  709. }
  710. }
  711. /**
  712. * Returns a string representation of this JLabel. This method
  713. * is intended to be used only for debugging purposes, and the
  714. * content and format of the returned string may vary between
  715. * implementations. The returned string may be empty but may not
  716. * be <code>null</code>.
  717. *
  718. * @return a string representation of this JLabel.
  719. */
  720. protected String paramString() {
  721. String textString = (text != null ?
  722. text : "");
  723. String defaultIconString = (defaultIcon != null ?
  724. defaultIcon.toString() : "");
  725. String disabledIconString = (disabledIcon != null ?
  726. disabledIcon.toString() : "");
  727. String labelForString = (labelFor != null ?
  728. labelFor.toString() : "");
  729. String verticalAlignmentString;
  730. if (verticalAlignment == TOP) {
  731. verticalAlignmentString = "TOP";
  732. } else if (verticalAlignment == CENTER) {
  733. verticalAlignmentString = "CENTER";
  734. } else if (verticalAlignment == BOTTOM) {
  735. verticalAlignmentString = "BOTTOM";
  736. } else verticalAlignmentString = "";
  737. String horizontalAlignmentString;
  738. if (horizontalAlignment == LEFT) {
  739. horizontalAlignmentString = "LEFT";
  740. } else if (horizontalAlignment == CENTER) {
  741. horizontalAlignmentString = "CENTER";
  742. } else if (horizontalAlignment == RIGHT) {
  743. horizontalAlignmentString = "RIGHT";
  744. } else horizontalAlignmentString = "";
  745. String verticalTextPositionString;
  746. if (verticalTextPosition == TOP) {
  747. verticalTextPositionString = "TOP";
  748. } else if (verticalTextPosition == CENTER) {
  749. verticalTextPositionString = "CENTER";
  750. } else if (verticalTextPosition == BOTTOM) {
  751. verticalTextPositionString = "BOTTOM";
  752. } else verticalTextPositionString = "";
  753. String horizontalTextPositionString;
  754. if (horizontalTextPosition == LEFT) {
  755. horizontalTextPositionString = "LEFT";
  756. } else if (horizontalTextPosition == CENTER) {
  757. horizontalTextPositionString = "CENTER";
  758. } else if (horizontalTextPosition == RIGHT) {
  759. horizontalTextPositionString = "RIGHT";
  760. } else horizontalTextPositionString = "";
  761. return super.paramString() +
  762. ",defaultIcon=" + defaultIconString +
  763. ",disabledIcon=" + disabledIconString +
  764. ",horizontalAlignment=" + horizontalAlignmentString +
  765. ",horizontalTextPosition=" + horizontalTextPositionString +
  766. ",iconTextGap=" + iconTextGap +
  767. ",labelFor=" + labelForString +
  768. ",text=" + textString +
  769. ",verticalAlignment=" + verticalAlignmentString +
  770. ",verticalTextPosition=" + verticalTextPositionString;
  771. }
  772. /**
  773. * --- Accessibility Support ---
  774. */
  775. /**
  776. * Get the AccessibleContext of this object
  777. *
  778. * @return the AccessibleContext of this object
  779. * @beaninfo
  780. * expert: true
  781. * description: The AccessibleContext associated with this Label.
  782. */
  783. public AccessibleContext getAccessibleContext() {
  784. if (accessibleContext == null) {
  785. accessibleContext = new AccessibleJLabel();
  786. }
  787. return accessibleContext;
  788. }
  789. /**
  790. * The class used to obtain the accessible role for this object.
  791. * <p>
  792. * <strong>Warning:</strong>
  793. * Serialized objects of this class will not be compatible with
  794. * future Swing releases. The current serialization support is appropriate
  795. * for short term storage or RMI between applications running the same
  796. * version of Swing. A future release of Swing will provide support for
  797. * long term persistence.
  798. */
  799. protected class AccessibleJLabel extends AccessibleJComponent {
  800. /**
  801. * Get the accessible name of this object.
  802. *
  803. * @return the localized name of the object -- can be null if this
  804. * object does not have a name
  805. * @see AccessibleContext#setAccessibleName
  806. */
  807. public String getAccessibleName() {
  808. if (accessibleName != null) {
  809. return accessibleName;
  810. } else {
  811. if (getText() == null) {
  812. return super.getAccessibleName();
  813. } else {
  814. return getText();
  815. }
  816. }
  817. }
  818. /**
  819. * Get the role of this object.
  820. *
  821. * @return an instance of AccessibleRole describing the role of the
  822. * object
  823. * @see AccessibleRole
  824. */
  825. public AccessibleRole getAccessibleRole() {
  826. return AccessibleRole.LABEL;
  827. }
  828. } // AccessibleJComponent
  829. /**
  830. * Get the component this is labelling.
  831. *
  832. * @return the Component this is labelling. Can be null if this
  833. * does not label a Component. If the displayedMnemonic
  834. * property is set and the labelFor property is also set, the label
  835. * will call the requestFocus method of the component specified by the
  836. * labelFor property when the mnemonic is activated.
  837. *
  838. * @see #getDisplayedMnemonic
  839. * @see #setDisplayedMnemonic
  840. */
  841. public Component getLabelFor() {
  842. return labelFor;
  843. }
  844. /**
  845. * Set the component this is labelling. Can be null if this does not
  846. * label a Component. If the displayedMnemonic property is set
  847. * and the labelFor property is also set, the label will
  848. * call the requestFocus method of the component specified by the
  849. * labelFor property when the mnemonic is activated.
  850. *
  851. * @param c the Component this label is for, or null if the label is
  852. * not the label for a component
  853. *
  854. * @see #getDisplayedMnemonic
  855. * @see #setDisplayedMnemonic
  856. *
  857. * @beaninfo
  858. * bound: true
  859. * description: The component this is labelling.
  860. */
  861. public void setLabelFor(Component c) {
  862. Component oldC = labelFor;
  863. labelFor = c;
  864. firePropertyChange("labelFor", oldC, c);
  865. if (oldC instanceof JComponent) {
  866. ((JComponent)oldC).putClientProperty(LABELED_BY_PROPERTY, null);
  867. }
  868. if (c instanceof JComponent) {
  869. ((JComponent)c).putClientProperty(LABELED_BY_PROPERTY, this);
  870. }
  871. }
  872. }