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 leading edge 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 on the trailing edge of the image, with the text and image vertically aligned.
A label's leading and trailing edge are determined from the value of its
java. property. At present, the default
ComponentOrientation setting maps the leading edge to left and the trailing
edge to right.
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
Swing is not thread safe. For more information see Swing's Threading Policy.
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. As of 1.4, support for long term storage
of all JavaBeans
has been added to the java.beans package.
Please see java..
| Modifier and Type | Class and Description |
|---|---|
| protected class | JLabel.
The class used to obtain the accessible role for this object. |
| Modifier and Type | Field and Description |
|---|---|
| private Icon | |
| private Icon | |
| private boolean | |
| private int | |
| private int | |
| private int | |
| pack-priv static final String | LABELED_BY_PROPERTY
Client property key used to determine what label is labeling the component. |
| protected Component | labelFor
The Component this label is for; null if the label is not the label for a component |
| private int | |
| private int | |
| private String | |
| private static final String | |
| private int | |
| private int |
| Access | Constructor and Description |
|---|---|
| public | JLabel(String
The text to be displayed by the label. text, Icon The image to be displayed by the label. icon, int One of the following constants
defined in horizontalAlignment)SwingConstants:
LEFT,
CENTER,
RIGHT,
LEADING or
TRAILING.Creates a |
| public | |
| public | |
| public | |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected int | Returns: the key value ifkey is a legal value for the
horizontalAlignment propertiesthe property value to check key, String the IllegalArgumentException detail message message)Verify that key is a legal value for the horizontalAlignment properties. |
| protected int | Returns: the key value ifkey is a legal value for the
verticalAlignment or verticalTextPosition propertiesthe property value to check key, String the IllegalArgumentException detail message message)Verify that key is a legal value for the verticalAlignment or verticalTextPosition properties. |
| public AccessibleContext | Returns: the AccessibleContext of this objectOverrides java. Implements javax. Get the AccessibleContext of this object |
| public Icon | Returns: thedisabledIcon propertyReturns the icon used by the label when it's disabled. |
| public int | Returns: int value for the mnemonic keyReturn the keycode that indicates a mnemonic key. |
| public int | Returns: index representing mnemonic characterReturns the character, as an index, that the look and feel should provide decoration for as representing the mnemonic character. |
| public int | Returns: The value of the horizontalAlignment property, one of the following constants defined inSwingConstants:
LEFT,
CENTER,
RIGHT,
LEADING or
TRAILING.Returns the alignment of the label's contents along the X axis. |
| public int | Returns: One of the following constants defined inSwingConstants:
LEFT,
CENTER,
RIGHT,
LEADING or
TRAILING.Returns the horizontal position of the label's text, relative to its image. |
| public Icon | |
| public int | Returns: an int equal to the number of pixels between the text and the icon.Returns the amount of space between the text and the icon displayed in this label. |
| public Component | 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.Get the component this is labelling. |
| public String | |
| public LabelUI | Returns: LabelUI objectOverrides javax. Returns the L&F object that renders this component. |
| public String | Returns: the string "LabelUI"Overrides javax. Returns a string that specifies the name of the l&f class that renders this component. |
| public int | Returns: The value of the verticalAlignment property, one of the following constants defined inSwingConstants:
TOP,
CENTER, or
BOTTOM.Returns the alignment of the label's contents along the Y axis. |
| public int | Returns: One of the following constants defined inSwingConstants:
TOP,
CENTER, or
BOTTOM.Returns the vertical position of the label's text, relative to its image. |
| public boolean | imageUpdate(Image
the img, int Image to be comparedflags used to repaint the label when the image
is updated and which determine how much is to be painted infoflags, int the x coordinate x, int the y coordinate y, int the width w, int the height h)Overrides java. Implements java. If the component is not showing or either the icon or disabled
icon is not an |
| protected String | Returns: a string representation of this JLabel.Overrides javax. Returns a string representation of this JLabel. |
| public void | setDisabledIcon(Icon
the Icon to display when the component is disabled disabledIcon)Set the icon to be displayed if this JLabel is "disabled" (JLabel.setEnabled(false)). |
| public void | setDisplayedMnemonic(int
a keycode that indicates a mnemonic key key)Specify a keycode that indicates a mnemonic key. |
| public void | setDisplayedMnemonic(char
a char specifying the mnemonic to display aChar)Specifies the displayedMnemonic as a char value. |
| public void | setDisplayedMnemonicIndex(int
Index into the String to underline index)Provides a hint to the look and feel as to which character in the text should be decorated to represent the mnemonic. |
| public void | setHorizontalAlignment(int
One of the following constants
defined in alignment)SwingConstants:
LEFT,
CENTER (the default for image-only labels),
RIGHT,
LEADING (the default for text-only labels) or
TRAILING.Sets the alignment of the label's contents along the X axis. |
| public void | setHorizontalTextPosition(int
One of the following constants
defined in textPosition)SwingConstants:
LEFT,
CENTER,
RIGHT,
LEADING, or
TRAILING (the default).Sets the horizontal position of the label's text, relative to its image. |
| public void | |
| public void | setIconTextGap(int
the space between the icon and text properties iconTextGap)If both the icon and text properties are set, this property defines the space between them. |
| public void | setLabelFor(Component
the Component this label is for, or null if the label is
not the label for a component c)Set the component this is labelling. |
| public void | |
| public void | |
| public void | setVerticalAlignment(int
One of the following constants
defined in alignment)SwingConstants:
TOP,
CENTER (the default), or
BOTTOM.Sets the alignment of the label's contents along the Y axis. |
| public void | setVerticalTextPosition(int
One of the following constants
defined in textPosition)SwingConstants:
TOP,
CENTER (the default), or
BOTTOM.Sets the vertical position of the label's text, relative to its image. |
| public void | updateUI()
Overrides javax. Resets the UI property to a value from the current look and feel. |
| private void | writeObject(ObjectOutputStream s)
Hides javax. See readObject() and writeObject() in JComponent for more information about serialization in Swing. |