javax.swing
Class JLabel

public class JLabel
extends JComponent
implements Accessible, SwingConstants
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 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 JavaBeansTM has been added to the java.beans package. Please see XMLEncoder .

SinceNot specified.
Version1.131 05/05/07
AuthorHans Muller
Wiki javadoc Use textile entry format.
Add your comments here.
Field Summary
protected Component labelFor
No description provided.
Constructor Summary
JLabel( String text, Icon icon, int horizontalAlignment )
Creates a JLabel instance with the specified text, image, and horizontal alignment.
JLabel( String text, int horizontalAlignment )
Creates a JLabel instance with the specified text and horizontal alignment.
JLabel( String text )
Creates a JLabel instance with the specified text.
JLabel( Icon image, int horizontalAlignment )
Creates a JLabel instance with the specified image and horizontal alignment.
JLabel( Icon image )
Creates a JLabel instance with the specified image.
JLabel()
Creates a JLabel instance with no image and with an empty string for the title.
Method Summary
protected int checkHorizontalKey( int key, String message )
Verify that key is a legal value for the horizontalAlignment properties.
protected int checkVerticalKey( int key, 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 icon used by the label when it's disabled.
int getDisplayedMnemonic()
Return the keycode that indicates a mnemonic key.
int getDisplayedMnemonicIndex()
Returns the character, as an index, that the look and feel should provide decoration for as representing the mnemonic character.
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.
Component getLabelFor()
Get the component this is labelling.
String getText()
Returns the text string that the label displays.
LabelUI getUI()
Returns the L&F object that renders this component.
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.
boolean imageUpdate( Image img, int infoflags, int x, int y, int w, int h )
This is overridden to return false if the current Icon's Image is not equal to the passed in Image img.
protected String paramString()
Returns a string representation of this JLabel.
void setDisabledIcon( Icon disabledIcon )
Set the icon to be displayed if this JLabel is "disabled" (JLabel.setEnabled(false)).
void setDisplayedMnemonic( int key )
Specify a keycode that indicates a mnemonic key.
void setDisplayedMnemonic( char aChar )
Specifies the displayedMnemonic as a char value.
void setDisplayedMnemonicIndex( int index )
Provides a hint to the look and feel as to which character in the text should be decorated to represent the mnemonic.
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( Component c )
Set the component this is labelling.
void setText( 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()
Resets the UI property to a value from the current look and feel.
Methods inherited from java.awtComponent
Methods inherited from java.awtContainer
Methods inherited from javax.swingJComponent
labelFor
protected Component labelFor
No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
JLabel
public JLabel ( 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 on the trailing edge of the image.
Parameters
TypeNameDescription
String text The text to be displayed by the label.
Icon icon The image to be displayed by the label.
int horizontalAlignment One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.
Wiki javadoc Use textile entry format.
Add your comments here.
JLabel
public JLabel ( 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
TypeNameDescription
String text The text to be displayed by the label.
int horizontalAlignment One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.
Wiki javadoc Use textile entry format.
Add your comments here.
JLabel
public JLabel ( String text )
Creates a JLabel instance with the specified text. The label is aligned against the leading edge of its display area, and centered vertically.
Parameters
TypeNameDescription
String text The text to be displayed by the label.
Wiki javadoc Use textile entry format.
Add your comments here.
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
TypeNameDescription
Icon image The image to be displayed by the label.
int horizontalAlignment One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING or TRAILING.
Wiki javadoc Use textile entry format.
Add your comments here.
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
TypeNameDescription
Icon image The image to be displayed by the label.
Wiki javadoc Use textile entry format.
Add your comments here.
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 on the leading edge of the label's display area.
Wiki javadoc Use textile entry format.
Add your comments here.
checkHorizontalKey
protected int checkHorizontalKey ( int key, String message )
Verify that key is a legal value for the horizontalAlignment properties.
Parameters
TypeNameDescription
int key the property value to check
String message the IllegalArgumentException detail message
Wiki javadoc Use textile entry format.
Add your comments here.
checkVerticalKey
protected int checkVerticalKey ( int key, String message )
Verify that key is a legal value for the verticalAlignment or verticalTextPosition properties.
Parameters
TypeNameDescription
int key the property value to check
String message the IllegalArgumentException detail message
Wiki javadoc Use textile entry format.
Add your comments here.
getAccessibleContext
public AccessibleContext getAccessibleContext ( )
Get the AccessibleContext of this object
Overrides method in JComponent
Wiki javadoc Use textile entry format.
Add your comments here.
getDisabledIcon
public Icon getDisabledIcon ( )
Returns the icon used by the label when it's disabled. If no disabled icon has been set this will forward the call to the look and feel to construct an appropriate disabled Icon.

Some look and feels might not render the disabled Icon, in which case they will ignore this.

Wiki javadoc Use textile entry format.
Add your comments here.
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.
Wiki javadoc Use textile entry format.
Add your comments here.
getDisplayedMnemonicIndex
public int getDisplayedMnemonicIndex ( )
Returns the character, as an index, that the look and feel should provide decoration for as representing the mnemonic character.
Since: 1.4
Wiki javadoc Use textile entry format.
Add your comments here.
getHorizontalAlignment
public int getHorizontalAlignment ( )
Returns the alignment of the label's contents along the X axis.
Wiki javadoc Use textile entry format.
Add your comments here.
getHorizontalTextPosition
public int getHorizontalTextPosition ( )
Returns the horizontal position of the label's text, relative to its image.
See also
Wiki javadoc Use textile entry format.
Add your comments here.
getIcon
public Icon getIcon ( )
Returns the graphic image (glyph, icon) that the label displays.
See also
Wiki javadoc Use textile entry format.
Add your comments here.
getIconTextGap
public int getIconTextGap ( )
Returns the amount of space between the text and the icon displayed in this label.
Wiki javadoc Use textile entry format.
Add your comments here.
getLabelFor
public Component getLabelFor ( )
Get the component this is labelling.
Wiki javadoc Use textile entry format.
Add your comments here.
getText
public String getText ( )
Returns the text string that the label displays.
See also
Wiki javadoc Use textile entry format.
Add your comments here.
getUI
public LabelUI getUI ( )
Returns the L&F object that renders this component.
Wiki javadoc Use textile entry format.
Add your comments here.
getUIClassID
public String getUIClassID ( )
Returns a string that specifies the name of the l&f class that renders this component.
Overrides method in JComponent
Wiki javadoc Use textile entry format.
Add your comments here.
getVerticalAlignment
public int getVerticalAlignment ( )
Returns the alignment of the label's contents along the Y axis.
Wiki javadoc Use textile entry format.
Add your comments here.
getVerticalTextPosition
public int getVerticalTextPosition ( )
Returns the vertical position of the label's text, relative to its image.
Wiki javadoc Use textile entry format.
Add your comments here.
imageUpdate
public boolean imageUpdate ( Image img, int infoflags, int x, int y, int w, int h )
This is overridden to return false if the current Icon's Image is not equal to the passed in Image img.
Overrides method in Component
Parameters
TypeNameDescription
Image img No description provided.
int infoflags No description provided.
int x No description provided.
int y No description provided.
int w No description provided.
int h No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
paramString
protected 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.
Overrides method in JComponent
Wiki javadoc Use textile entry format.
Add your comments here.
setDisabledIcon
public void setDisabledIcon ( Icon disabledIcon )
Set the icon to be displayed if this JLabel is "disabled" (JLabel.setEnabled(false)).

The default value of this property is null.

Parameters
TypeNameDescription
Icon disabledIcon the Icon to display when the component is disabled
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
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.
Parameters
TypeNameDescription
int key No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setDisplayedMnemonic
public void setDisplayedMnemonic ( char aChar )
Specifies the displayedMnemonic as a char value.
Parameters
TypeNameDescription
char aChar a char specifying the mnemonic to display
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setDisplayedMnemonicIndex
public void setDisplayedMnemonicIndex ( int index )
Provides a hint to the look and feel as to which character in the text should be decorated to represent the mnemonic. Not all look and feels may support this. A value of -1 indicates either there is no mnemonic, the mnemonic character is not contained in the string, or the developer does not wish the mnemonic to be displayed.

The value of this is updated as the properties relating to the mnemonic change (such as the mnemonic itself, the text...). You should only ever have to call this if you do not wish the default character to be underlined. For example, if the text was 'Save As', with a mnemonic of 'a', and you wanted the 'A' to be decorated, as 'Save As', you would have to invoke setDisplayedMnemonicIndex(5) after invoking setDisplayedMnemonic(KeyEvent.VK_A).

Since: 1.4
Parameters
TypeNameDescription
int index Index into the String to underline
Returns void No description provided.
Exceptions
IllegalArgumentException will be thrown if index= length of the text, or < -1
Wiki javadoc Use textile entry format.
Add your comments here.
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
TypeNameDescription
int alignment 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.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setHorizontalTextPosition
public void setHorizontalTextPosition ( int textPosition )
Sets the horizontal position of the label's text, relative to its image.
Parameters
TypeNameDescription
int textPosition One of the following constants defined in SwingConstants: LEFT, CENTER, RIGHT, LEADING, or TRAILING (the default).
Returns void No description provided.
See also
Wiki javadoc Use textile entry format.
Add your comments here.
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.

Parameters
TypeNameDescription
Icon icon No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
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.

Parameters
TypeNameDescription
int iconTextGap No description provided.
Returns void No description provided.
See also
Wiki javadoc Use textile entry format.
Add your comments here.
setLabelFor
public void setLabelFor ( 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
TypeNameDescription
Component c the Component this label is for, or null if the label is not the label for a component
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setText
public void setText ( 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.

Parameters
TypeNameDescription
String text No description provided.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
setUI
public void setUI ( LabelUI ui )
Sets the L&F object that renders this component.
Parameters
TypeNameDescription
LabelUI ui the LabelUI L&F object
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
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
TypeNameDescription
int alignment One of the following constants defined in SwingConstants: TOP, CENTER (the default), or BOTTOM.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
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
TypeNameDescription
int textPosition One of the following constants defined in SwingConstants: TOP, CENTER (the default), or BOTTOM.
Returns void No description provided.
Wiki javadoc Use textile entry format.
Add your comments here.
updateUI
public void updateUI ( )
Resets the UI property to a value from the current look and feel.
Overrides method in JComponent
Returns void No description provided.
See also
Wiki javadoc Use textile entry format.
Add your comments here.