APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JLabel

extends JComponent
implements SwingConstants, Accessible
Class Inheritance
All Implemented Interfaces
javax.accessibility.Accessible, javax.swing.SwingConstants
Known Direct Subclasses
javax.swing.plaf.basic.BasicComboBoxRenderer, javax.swing.plaf.metal.MetalFileChooserUI.AlignedLabel, javax.swing.plaf.synth.SynthComboBoxUI.SynthComboBoxRenderer, javax.swing.table.DefaultTableCellRenderer, javax.swing.tree.DefaultTreeCellRenderer, com.sun.java.swing.plaf.gtk.GTKColorChooserPanel.OpaqueLabel, sun.swing.plaf.synth.SynthFileChooserUIImpl.AlignedLabel, javax.swing.DefaultListCellRenderer
Annotations
@JavaBean
defaultProperty:UI
description:A component that displays a short string and an icon.
@SwingContainer:false
@SuppressWarnings:serial
Imports
java.awt.Component, .Image, .Insets, .Point, .Rectangle, .Shape, java.awt.geom.Rectangle2D, java.beans.BeanProperty, .JavaBean, .Transient, java.io.IOException, .ObjectOutputStream, .Serial, java.text.BreakIterator, javax.accessibility.Accessible, .AccessibleContext, .AccessibleExtendedComponent, .AccessibleIcon, .AccessibleKeyBinding, .AccessibleRelation, .AccessibleRelationSet, .AccessibleRole, .AccessibleText, javax.swing.plaf.LabelUI, javax.swing.text.AttributeSet, .BadLocationException, .Document, .Element, .Position, .StyledDocument, .View

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 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.awt.ComponentOrientation 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.beans.XMLEncoder.

Author
Hans Muller
Since
1.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JLabel.AccessibleJLabel

The class used to obtain the accessible role for this object.

Field Summary

Modifier and TypeField 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
Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor 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 SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.
horizontalAlignment
)

Creates a JLabel instance with the specified text, image, and horizontal alignment.

public
JLabel(String
The text to be displayed by the label.
text
,
int
One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.
horizontalAlignment
)

Creates a JLabel instance with the specified text and horizontal alignment.

public
JLabel(String
The text to be displayed by the label.
text
)

Creates a JLabel instance with the specified text.

public
JLabel(Icon
The image to be displayed by the label.
image
,
int
One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.
horizontalAlignment
)

Creates a JLabel instance with the specified image and horizontal alignment.

public
JLabel(Icon
The image to be displayed by the label.
image
)

Creates a JLabel instance with the specified image.

public
JLabel()

Creates a JLabel instance with no image and with an empty string for the title.

Method Summary

Modifier and TypeMethod and Description
protected int

Returns:

the key value if key is a legal value for the horizontalAlignment properties
checkHorizontalKey
(int
the 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 if key is a legal value for the verticalAlignment or verticalTextPosition properties
checkVerticalKey
(int
the 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 object
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Get the AccessibleContext of this object

public Icon

Returns:

the disabledIcon property
getDisabledIcon
()

Returns the icon used by the label when it's disabled.

public int

Returns:

int value for the mnemonic key
getDisplayedMnemonic
()

Return the keycode that indicates a mnemonic key.

public int

Returns:

index representing mnemonic character
getDisplayedMnemonicIndex
()

Returns 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 in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.
getHorizontalAlignment
()

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

public int

Returns:

One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.
getHorizontalTextPosition
()

Returns the horizontal position of the label's text, relative to its image.

public Icon

Returns:

an Icon
getIcon
()

Returns the graphic image (glyph, icon) that the label displays.

public int

Returns:

an int equal to the number of pixels between the text and the icon.
getIconTextGap
()

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.
getLabelFor
()

Get the component this is labelling.

public String

Returns:

a String
getText
()

Returns the text string that the label displays.

public LabelUI

Returns:

LabelUI object
getUI
()

Overrides javax.swing.JComponent.getUI.

Returns the L&F object that renders this component.

public String

Returns:

the string "LabelUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

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 in SwingConstants: TOP, CENTER, or BOTTOM.
getVerticalAlignment
()

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

public int

Returns:

One of the following constants defined in SwingConstants: TOP, CENTER, or BOTTOM.
getVerticalTextPosition
()

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

public boolean
imageUpdate(Image
the Image to be compared
img
,
int
flags 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.awt.Component.imageUpdate.

Implements java.awt.image.ImageObserver.imageUpdate.

If the component is not showing or either the icon or disabled icon is not an ImageIcon with an Image equal to the passed in Image, return false; otherwise it will delegate to the super-class.

protected String

Returns:

a string representation of this JLabel.
paramString
()

Overrides javax.swing.JComponent.paramString.

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 SwingConstants: LEFT, CENTER (the default for image-only labels), RIGHT, LEADING (the default for text-only labels) or TRAILING.
alignment
)

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

public void
setHorizontalTextPosition(int
One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING, or TRAILING (the default).
textPosition
)

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

public void
setIcon(Icon
the default icon this component will display
icon
)

Defines the icon this component will display.

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
setText(String
the single line of text this component will display
text
)

Defines the single line of text this component will display.

public void
setUI(LabelUI
the LabelUI L&F object
ui
)

Sets the L&F object that renders this component.

public void
setVerticalAlignment(int
One of the following constants defined in SwingConstants: TOP, CENTER (the default), or BOTTOM.
alignment
)

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

public void
setVerticalTextPosition(int
One of the following constants defined in SwingConstants: TOP, CENTER (the default), or BOTTOM.
textPosition
)

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

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Resets the UI property to a value from the current look and feel.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject() and writeObject() in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcompWriteObjectNotifycontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintBorderpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetEnabledsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate