Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Swing 1.1
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## javax.swing
Class JLabel

    java.lang.Object
      |
      +--java.awt.Component
            |
            +--java.awt.Container
                  |
                  +--javax.swing.JComponent
                        |
                        +--javax.swing.JLabel

Direct Known Subclasses:
  BasicComboBoxRenderer, DefaultListCellRenderer, DefaultTableCellRenderer, DefaultTreeCellRenderer

public class JLabel
extends JComponent
implements SwingConstants, Accessible

A display area for a short text string or an image, or both. A label does not react to input events. As a result, it cannot get the keyboard focus. A label can, however, display a keyboard alternative as a convenience for a nearby component that has a keyboard alternative but can't display it.

A `JLabel` object can display either text, an image, or both. You can specify where in the label's display area the label's contents are aligned by setting the vertical and horizontal alignment. By default, labels are vertically centered in their display area. Text-only labels are left-aligned, by default; image-only labels are horizontally centered, by default.

You can also specify the position of the text relative to the image. By default, text is to the right of the image, with the text and image vertically aligned.

Finally, you can use the `setIconTextGap` method to specify how many pixels should appear between the text and the image. The default is 4 pixels.

See How to Use Labels in The Java Tutorial for further documentation.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. A future release of Swing will provide support for long term persistence.

See Also:
  Serialized Form

Inner Class Summary
`protected class` | `JLabel.AccessibleJLabel` The class used to obtain the accessible role for this object.

Inner classes inherited from class javax.swing.JComponent
`JComponent.AccessibleJComponent`

Field Summary
`protected java.awt.Component` | `labelFor`

Fields inherited from class javax.swing.JComponent
`accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW`

Fields inherited from class java.awt.Component
`BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT`

Constructor Summary
`JLabel()` Creates a `JLabel` instance with no image and with an empty string for the title.
`JLabel(Icon image)` Creates a `JLabel` instance with the specified image.
`JLabel(Icon image, int horizontalAlignment)` Creates a `JLabel` instance with the specified image and horizontal alignment.
`JLabel(java.lang.String text)` Creates a `JLabel` instance with the specified text.
`JLabel(java.lang.String text, Icon icon, int horizontalAlignment)` Creates a `JLabel` instance with the specified text, image, and horizontal alignment.
`JLabel(java.lang.String text, int horizontalAlignment)` Creates a `JLabel` instance with the specified text and horizontal alignment.

Method Summary
`protected int` | `checkHorizontalKey(int key, java.lang.String message)` Verify that key is a legal value for the horizontalAlignment properties.
`protected int` | `checkVerticalKey(int key, java.lang.String message)` Verify that key is a legal value for the verticalAlignment or verticalTextPosition properties.
`AccessibleContext` | `getAccessibleContext()` Get the AccessibleContext of this object
`Icon` | `getDisabledIcon()` Returns the value of the disabledIcon property if it's been set, If it hasn't been set and the value of the icon property is an ImageIcon, we compute a "grayed out" version of the icon and update the disabledIcon property with that.
`int` | `getDisplayedMnemonic()` Return the keycode that indicates a mnemonic key.
`int` | `getHorizontalAlignment()` Returns the alignment of the label's contents along the X axis.
`int` | `getHorizontalTextPosition()` Returns the horizontal position of the label's text, relative to its image.
`Icon` | `getIcon()` Returns the graphic image (glyph, icon) that the label displays.
`int` | `getIconTextGap()` Returns the amount of space between the text and the icon displayed in this label.
`java.awt.Component` | `getLabelFor()` Get the component this is labelling.
`java.lang.String` | `getText()` Returns the text string that the label displays.
`LabelUI` | `getUI()` Returns the L&F object that renders this component.
`java.lang.String` | `getUIClassID()` Returns a string that specifies the name of the l&f class that renders this component.
`int` | `getVerticalAlignment()` Returns the alignment of the label's contents along the Y axis.
`int` | `getVerticalTextPosition()` Returns the vertical position of the label's text, relative to its image.
`protected java.lang.String` | `paramString()` Returns a string representation of this JLabel.
`void` | `setDisabledIcon(Icon disabledIcon)` Set the icon to be displayed if this JLabel is "disabled", i.e.
`void` | `setDisplayedMnemonic(char aChar)` Specifies the displayedMnemonic as a char value.
`void` | `setDisplayedMnemonic(int key)` Specify a keycode that indicates a mnemonic key.
`void` | `setHorizontalAlignment(int alignment)` Sets the alignment of the label's contents along the X axis.
`void` | `setHorizontalTextPosition(int textPosition)` Sets the horizontal position of the label's text, relative to its image.
`void` | `setIcon(Icon icon)` Defines the icon this component will display.
`void` | `setIconTextGap(int iconTextGap)` If both the icon and text properties are set, this property defines the space between them.
`void` | `setLabelFor(java.awt.Component c)` Set the component this is labelling.
`void` | `setText(java.lang.String text)` Defines the single line of text this component will display.
`void` | `setUI(LabelUI ui)` Sets the L&F object that renders this component.
`void` | `setVerticalAlignment(int alignment)` Sets the alignment of the label's contents along the Y axis.
`void` | `setVerticalTextPosition(int textPosition)` Sets the vertical position of the label's text, relative to its image.
`void` | `updateUI()` Notification from the UIFactory that the L&F has changed.

Methods inherited from class javax.swing.JComponent
`addAncestorListener, addNotify, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getGraphics, getHeight, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, processComponentKeyEvent, processFocusEvent, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setEnabled, setFont, setForeground, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVisible, unregisterKeyboardAction, update`

Methods inherited from class java.awt.Container
`add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setLayout, validate, validateTree`

Methods inherited from class java.awt.Component
`action, add, addComponentListener, addFocusListener, addKeyListener, addMouseListener, addMouseMotionListener, bounds, checkImage, checkImage, contains, createImage, createImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, getBackground, getBounds, getColorModel, getCursor, getFont, getFontMetrics, getForeground, getLocale, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hide, imageUpdate, inside, isEnabled, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Field Detail

### labelFor

    protected java.awt.Component labelFor

Constructor Detail

### JLabel

    public JLabel(java.lang.String text,
                  Icon icon,
                  int horizontalAlignment)

  Creates a `JLabel` instance with the specified text, image, and horizontal alignment. The label is centered vertically in its display area. The text is to the right of the image.
  Parameters:
    `text` - The text to be displayed by the label.
    `icon` - The image to be displayed by the label.
    `horizontalAlignment` - One of the following constants defined in `SwingConstants`: `LEFT`, `CENTER`, or `RIGHT`.

### JLabel

    public JLabel(java.lang.String text,
                  int horizontalAlignment)

  Creates a `JLabel` instance with the specified text and horizontal alignment. The label is centered vertically in its display area.
  Parameters:
    `text` - The text to be displayed by the label.
    `horizontalAlignment` - One of the following constants defined in `SwingConstants`: `LEFT`, `CENTER`, or `RIGHT`.

### JLabel

    public JLabel(java.lang.String text)

  Creates a `JLabel` instance with the specified text. The label is aligned against the left side of its display area, and centered vertically.
  Parameters:
    `text` - The text to be displayed by the label.

### JLabel

    public JLabel(Icon image,
                  int horizontalAlignment)

  Creates a `JLabel` instance with the specified image and horizontal alignment. The label is centered vertically in its display area.
  Parameters:
    `icon` - The image to be displayed by the label.
    `horizontalAlignment` - One of the following constants defined in `SwingConstants`: `LEFT`, `CENTER`, or `RIGHT`.

### JLabel

    public JLabel(Icon image)

  Creates a `JLabel` instance with the specified image. The label is centered vertically and horizontally in its display area.
  Parameters:
    `icon` - The image to be displayed by the label.

### JLabel

    public JLabel()

  Creates a `JLabel` instance with no image and with an empty string for the title. The label is centered vertically in its display area. The label's contents, once set, will be displayed at the left of the label's display area.

Method Detail

### getUI

    public LabelUI getUI()

  Returns the L&F object that renders this component.
  Returns:
    LabelUI object

### setUI

    public void setUI(LabelUI ui)

  Sets the L&F object that renders this component.
  Parameters:
    `ui` - the LabelUI L&F object
  See Also:
    `UIDefaults.getUI(javax.swing.JComponent)`

### updateUI

    public void updateUI()

  Notification from the UIFactory that the L&F has changed.
  Overrides:
    updateUI in class JComponent
  See Also:
    `JComponent.updateUI()`

### getUIClassID

    public java.lang.String getUIClassID()

  Returns a string that specifies the name of the l&f class that renders this component.
  Returns:
    String "LabelUI"
  Overrides:
    getUIClassID in class JComponent
  See Also:
    `JComponent.getUIClassID()`, `UIDefaults.getUI(javax.swing.JComponent)`

### getText

    public java.lang.String getText()

  Returns the text string that the label displays.
  Returns:
    a String
  See Also:
    `setText(java.lang.String)`

### setText

    public void setText(java.lang.String text)

  Defines the single line of text this component will display. If the value of text is null or empty string, nothing is displayed.

  The default value of this property is null.

  This is a JavaBeans bound property.

  See Also:
    `setVerticalTextPosition(int)`, `setHorizontalTextPosition(int)`, `setIcon(javax.swing.Icon)`

### getIcon

    public Icon getIcon()

  Returns the graphic image (glyph, icon) that the label displays.
  Returns:
    an Icon
  See Also:
    `setIcon(javax.swing.Icon)`

### setIcon

    public void setIcon(Icon icon)

  Defines the icon this component will display. If the value of icon is null, nothing is displayed.

  The default value of this property is null.

  This is a JavaBeans bound property.

  See Also:
    `setVerticalTextPosition(int)`, `setHorizontalTextPosition(int)`, `getIcon()`

### getDisabledIcon

    public Icon getDisabledIcon()

  Returns the value of the disabledIcon property if it's been set, If it hasn't been set and the value of the icon property is an ImageIcon, we compute a "grayed out" version of the icon and update the disabledIcon property with that.
  Returns:
    The value of the disabledIcon property.
  See Also:
    `setDisabledIcon(javax.swing.Icon)`, `ImageIcon`

### setDisabledIcon

    public void setDisabledIcon(Icon disabledIcon)

  Set the icon to be displayed if this JLabel is "disabled", i.e. JLabel.setEnabled(false).

  The default value of this property is null.

  Parameters:
    `disabledIcon` - the Icon to display when the component is disabled
  See Also:
    `getDisabledIcon()`, `JComponent.setEnabled(boolean)`

### setDisplayedMnemonic

    public void setDisplayedMnemonic(int key)

  Specify a keycode that indicates a mnemonic key. This property is used when the label is part of a larger component. If the labelFor property of the label is not null, the label will call the requestFocus method of the component specified by the labelFor property when the mnemonic is activated.
  See Also:
    `getLabelFor()`, `setLabelFor(java.awt.Component)`

### setDisplayedMnemonic

    public void setDisplayedMnemonic(char aChar)

  Specifies the displayedMnemonic as a char value.
  Parameters:
    `aChar` - a char specifying the mnemonic to display
  See Also:
    `setDisplayedMnemonic(int)`

### getDisplayedMnemonic

    public int getDisplayedMnemonic()

  Return the keycode that indicates a mnemonic key. This property is used when the label is part of a larger component. If the labelFor property of the label is not null, the label will call the requestFocus method of the component specified by the labelFor property when the mnemonic is activated.
  Returns:
    int value for the mnemonic key
  See Also:
    `getLabelFor()`, `setLabelFor(java.awt.Component)`

### checkHorizontalKey

    protected int checkHorizontalKey(int key,
                                     java.lang.String message)

  Verify that key is a legal value for the horizontalAlignment properties.
  Parameters:
    `key` - the property value to check
    `message` - the IllegalArgumentException detail message
  Throws:
    java.lang.IllegalArgumentException - if key isn't LEFT, CENTER, RIGHT, LEADING or TRAILING.
  See Also:
    `setHorizontalTextPosition(int)`, `setHorizontalAlignment(int)`

### checkVerticalKey

    protected int checkVerticalKey(int key,
                                   java.lang.String message)

  Verify that key is a legal value for the verticalAlignment or verticalTextPosition properties.
  Parameters:
    `key` - the property value to check
    `message` - the IllegalArgumentException detail message
  Throws:
    java.lang.IllegalArgumentException - if key isn't TOP, CENTER, or BOTTOM.
  See Also:
    `setVerticalAlignment(int)`, `setVerticalTextPosition(int)`

### getIconTextGap

    public int getIconTextGap()

  Returns the amount of space between the text and the icon displayed in this label.
  Returns:
    an int equal to the number of pixels between the text and the icon.
  See Also:
    `setIconTextGap(int)`

### setIconTextGap

    public void setIconTextGap(int iconTextGap)

  If both the icon and text properties are set, this property defines the space between them.

  The default value of this property is 4 pixels.

  This is a JavaBeans bound property.

  See Also:
    `getIconTextGap()`

### getVerticalAlignment

    public int getVerticalAlignment()

  Returns the alignment of the label's contents along the Y axis.
  Returns:
    The value of the verticalAlignment property, one of the following constants defined in `SwingConstants`: `TOP`, `CENTER`, or `BOTTOM`.
  See Also:
    `SwingConstants`, `setVerticalAlignment(int)`

### setVerticalAlignment

    public void setVerticalAlignment(int alignment)

  Sets the alignment of the label's contents along the Y axis.

  The default value of this property is CENTER.

  Parameters:
    `alignment` - One of the following constants defined in `SwingConstants`: `TOP`, `CENTER` (the default), or `BOTTOM`.
  See Also:
    `SwingConstants`, `getVerticalAlignment()`

### getHorizontalAlignment

    public int getHorizontalAlignment()

  Returns the alignment of the label's contents along the X axis.
  Returns:
    The value of the horizontalAlignment property, one of the following constants defined in `SwingConstants`: `LEFT`, `CENTER`, or `RIGHT`.
  See Also:
    `setHorizontalAlignment(int)`, `SwingConstants`

### setHorizontalAlignment

    public void setHorizontalAlignment(int alignment)

  Sets the alignment of the label's contents along the X axis.

  This is a JavaBeans bound property.

  Parameters:
    `alignment` - One of the following constants defined in `SwingConstants`: `LEFT` (the default for text-only labels), `CENTER` (the default for image-only labels), `RIGHT`, `LEADING` or `TRAILING`.
  See Also:
    `SwingConstants`, `getHorizontalAlignment()`

### getVerticalTextPosition

    public int getVerticalTextPosition()

  Returns the vertical position of the label's text, relative to its image.
  Returns:
    One of the following constants defined in `SwingConstants`: `TOP`, `CENTER`, or `BOTTOM`.
  See Also:
    `setVerticalTextPosition(int)`, `SwingConstants`

### setVerticalTextPosition

    public void setVerticalTextPosition(int textPosition)

  Sets the vertical position of the label's text, relative to its image.

  The default value of this property is CENTER.

  This is a JavaBeans bound property.

  Parameters:
    `textPosition` - One of the following constants defined in `SwingConstants`: `TOP`, `CENTER` (the default), or `BOTTOM`.
  See Also:
    `SwingConstants`, `getVerticalTextPosition()`

### getHorizontalTextPosition

    public int getHorizontalTextPosition()

  Returns the horizontal position of the label's text, relative to its image.
  Returns:
    One of the following constants defined in `SwingConstants`: `LEFT`, `CENTER`, or `RIGHT`.
  See Also:
    `SwingConstants`

### setHorizontalTextPosition

    public void setHorizontalTextPosition(int textPosition)

  Sets the horizontal position of the label's text, relative to its image.
  Parameters:
    `x` - One of the following constants defined in `SwingConstants`: `LEFT`, `CENTER`, `RIGHT` (the default), `LEADING`, or `TRAILING`.
  Throws:
    java.lang.IllegalArgumentException -
  See Also:
    `SwingConstants`

### paramString

    protected java.lang.String paramString()

  Returns a string representation of this JLabel. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be `null`.

  Overriding paramString() to provide information about the specific new aspects of the JFC components.

  Returns:
    a string representation of this JLabel.
  Overrides:
    paramString in class JComponent

### getAccessibleContext

    public AccessibleContext getAccessibleContext()

  Get the AccessibleContext of this object
  Specified by:
    getAccessibleContext in interface Accessible
  Returns:
    the AccessibleContext of this object
  Overrides:
    getAccessibleContext in class JComponent

### getLabelFor

    public java.awt.Component getLabelFor()

  Get the component this is labelling.
  Returns:
    the Component this is labelling. Can be null if this does not label a Component. If the displayedMnemonic property is set and the labelFor property is also set, the label will call the requestFocus method of the component specified by the labelFor property when the mnemonic is activated.
  See Also:
    `getDisplayedMnemonic()`, `setDisplayedMnemonic(int)`

### setLabelFor

    public void setLabelFor(java.awt.Component c)

  Set the component this is labelling. Can be null if this does not label a Component. If the displayedMnemonic property is set and the labelFor property is also set, the label will call the requestFocus method of the component specified by the labelFor property when the mnemonic is activated.
  Parameters:
    `c` - the Component this label is for, or null if the label is not the label for a component
  See Also:
    `getDisplayedMnemonic()`, `setDisplayedMnemonic(int)`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| Swing 1.1
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Submit a bug or feature
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.