javax.swing

Class JLabel

Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable, SwingConstants
Known Direct Subclasses:
BasicComboBoxRenderer, DefaultListCellRenderer, DefaultTableCellRenderer, DefaultTreeCellRenderer

public class JLabel
extends JComponent
implements Accessible, SwingConstants

A component that displays a static text message and/or an icon.
See Also:
Serialized Form

Nested Class Summary

protected class
JLabel.AccessibleJLabel
Provides the accessibility features for the JLabel component.

Nested classes/interfaces inherited from class javax.swing.JComponent

JComponent.AccessibleJComponent

Nested classes/interfaces inherited from class java.awt.Container

Container.AccessibleAWTContainer

Nested classes/interfaces inherited from class java.awt.Component

Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy

Field Summary

protected Component
labelFor
The Component the label will give focus to when its mnemonic is activated.

Fields inherited from class javax.swing.JComponent

TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, accessibleContext, listenerList, ui

Fields inherited from class java.awt.Component

BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT

Fields inherited from interface java.awt.image.ImageObserver

ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH

Fields inherited from interface javax.swing.SwingConstants

BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST

Constructor Summary

JLabel()
Creates a new vertically centered, horizontally on the leading edge JLabel object with text and no icon.
JLabel(String text)
Creates a new horizontally leading and vertically centered JLabel object with no icon and the given text.
JLabel(String text, int horizontalAlignment)
Creates a new vertically centered JLabel object with no icon and the given text and horizontal alignment.
JLabel(String text, Icon icon, int horizontalAlignment)
Creates a new vertically centered JLabel object with the given text, icon, and horizontal alignment.
JLabel(Icon image)
Creates a new vertically and horizontally centered JLabel object with no text and the given icon.
JLabel(Icon image, int horizontalAlignment)
Creates a new vertically centered JLabel object with no text and the given icon and horizontal alignment.

Method Summary

protected int
checkHorizontalKey(int key, String message)
Checks the specified key to ensure that it is valid as a horizontal alignment, throwing an IllegalArgumentException if the key is invalid.
protected int
checkVerticalKey(int key, String message)
Checks the specified key to ensure that it is valid as a vertical alignment, throwing an IllegalArgumentException if the key is invalid.
AccessibleContext
getAccessibleContext()
Returns the object that provides accessibility features for this JLabel component.
Icon
getDisabledIcon()
Returns the disabled icon.
int
getDisplayedMnemonic()
Returns the keycode that is used for the label's mnemonic.
int
getDisplayedMnemonicIndex()
Returns the index of the character in the label's text that will be underlined (to indicate that it is the mnemonic character), or -1 if no character is to be underlined.
int
getHorizontalAlignment()
Returns the horizontal alignment of the label (one of SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT, SwingConstants.LEADING and SwingConstants.TRAILING).
int
getHorizontalTextPosition()
Returns the horizontal position of the label's text relative to the icon.
Icon
getIcon()
Returns the active icon.
int
getIconTextGap()
Returns the gap between the icon and the text.
Component
getLabelFor()
Returns the component that this JLabel is providing the label for.
String
getText()
Returns the text displayed by the label.
LabelUI
getUI()
Returns the label's UI delegate.
String
getUIClassID()
Returns a name to identify which look and feel class will be the UI delegate for this label.
int
getVerticalAlignment()
Returns the vertical alignment of the label (one of SwingConstants.TOP, SwingConstants.CENTER and SwingConstants.BOTTOM).
int
getVerticalTextPosition()
Returns the vertical position of the label's text relative to the icon.
boolean
imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
Returns false if the current icon image (current icon will depend on whether the label is enabled) is not equal to the passed in image.
protected String
paramString()
Returns a string describing the attributes for the JLabel component, for use in debugging.
void
setDisabledIcon(Icon newIcon)
Sets the icon displayed when the label is disabled (this is a bound property with the name 'disabledIcon').
void
setDisplayedMnemonic(char mnemonic)
Sets the character that will be the label's mnemonic.
void
setDisplayedMnemonic(int mnemonic)
Sets the keycode that will be the label's mnemonic (this is a bound property with the name 'displayedMnemonic').
void
setDisplayedMnemonicIndex(int newIndex)
Sets the index of the character in the text that will be underlined to indicate that it is the mnemonic character for the label.
void
setFont(Font f)
Sets the font for the label (this a bound property with the name 'font').
void
setHorizontalAlignment(int alignment)
Sets the horizontal alignment for the label (this is a bound property with the name 'horizontalAlignment').
void
setHorizontalTextPosition(int textPosition)
Sets the horizontal position of the label's text relative to the icon (this is a bound property with the name 'horizontalTextPosition').
void
setIcon(Icon newIcon)
Sets the icon for the label (this is a bound property with the name 'icon').
void
setIconTextGap(int newGap)
Sets the gap between the icon and the text, in the case that both are visible (this is a bound property with the name 'iconTextGap').
void
setLabelFor(Component c)
Sets the component that this JLabel is providing the label for (this is a bound property with the name 'labelFor').
void
setText(String newText)
Sets the text for the label and sends a PropertyChangeEvent (with the name 'text') to all registered listeners.
void
setUI(LabelUI ui)
Sets the label's UI delegate.
void
setVerticalAlignment(int alignment)
Sets the vertical alignment for the label (this is a bound property with the name 'verticalAlignment').
void
setVerticalTextPosition(int textPosition)
Sets the vertical position of the label's text relative to the icon (this is a bound property with the name 'verticalTextPosition').
void
updateUI()
Resets the label's UI delegate to the default UI for the current look and feel.

Methods inherited from class javax.swing.JComponent

addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

labelFor

protected Component labelFor
The Component the label will give focus to when its mnemonic is activated.

Constructor Details

JLabel

public JLabel()
Creates a new vertically centered, horizontally on the leading edge JLabel object with text and no icon.

JLabel

public JLabel(String text)
Creates a new horizontally leading and vertically centered JLabel object with no icon and the given text.
Parameters:
text - The text to use with the label, null permitted.

JLabel

public JLabel(String text,
              int horizontalAlignment)
Creates a new vertically centered JLabel object with no icon and the given text and horizontal alignment.
Parameters:
text - The text to use with the label, null permitted.
horizontalAlignment - The horizontal alignment of the label, must be either CENTER, LEFT, RIGHT, LEADING or TRAILING.

JLabel

public JLabel(String text,
              Icon icon,
              int horizontalAlignment)
Creates a new vertically centered JLabel object with the given text, icon, and horizontal alignment.
Parameters:
text - The text to use with the label, null permitted.
icon - The icon to use with the label, null premitted.
horizontalAlignment - The horizontal alignment of the label, must be either CENTER, LEFT, RIGHT, LEADING or TRAILING.

JLabel

public JLabel(Icon image)
Creates a new vertically and horizontally centered JLabel object with no text and the given icon.
Parameters:
image - The icon to use with the label, null permitted.

JLabel

public JLabel(Icon image,
              int horizontalAlignment)
Creates a new vertically centered JLabel object with no text and the given icon and horizontal alignment. By default, the text is TRAILING the image.
Parameters:
image - The icon to use with the label, null premitted.
horizontalAlignment - The horizontal alignment of the label, must be either CENTER, LEFT, RIGHT, LEADING or TRAILING.

Method Details

checkHorizontalKey

protected int checkHorizontalKey(int key,
                                 String message)
Checks the specified key to ensure that it is valid as a horizontal alignment, throwing an IllegalArgumentException if the key is invalid. Valid keys are SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT, SwingConstants.LEADING and SwingConstants.TRAILING.
Parameters:
key - The key to check.
message - The message of the exception to be thrown if the key is invalid.
Returns:
The key if it is valid.
Throws:
IllegalArgumentException - If the key is invalid.

checkVerticalKey

protected int checkVerticalKey(int key,
                               String message)
Checks the specified key to ensure that it is valid as a vertical alignment, throwing an IllegalArgumentException if the key is invalid. Valid keys are SwingConstants.TOP, SwingConstants.CENTER and SwingConstants.BOTTOM.
Parameters:
key - The key to check.
message - The message of the exception to be thrown if the key is invalid.
Returns:
The key if it is valid.
Throws:
IllegalArgumentException - If the key is invalid.

getAccessibleContext

public AccessibleContext getAccessibleContext()
Returns the object that provides accessibility features for this JLabel component.
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in interface JComponent
Returns:
The accessible context (an instance of JLabel.AccessibleJLabel).

getDisabledIcon

public Icon getDisabledIcon()
Returns the disabled icon. The disabled icon is painted when the label is disabled. If the disabled icon is null and the active icon is an ImageIcon, this method returns a grayed version of the icon. The grayed version of the icon becomes the disabledIcon.
Returns:
The disabled icon.

getDisplayedMnemonic

public int getDisplayedMnemonic()
Returns the keycode that is used for the label's mnemonic.
Returns:
The keycode that is used for the label's mnemonic.

getDisplayedMnemonicIndex

public int getDisplayedMnemonicIndex()
Returns the index of the character in the label's text that will be underlined (to indicate that it is the mnemonic character), or -1 if no character is to be underlined.
Returns:
The index of the character that will be underlined.
Since:
1.4

getHorizontalAlignment

public int getHorizontalAlignment()
Returns the horizontal alignment of the label (one of SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT, SwingConstants.LEADING and SwingConstants.TRAILING). The default value depends on the installed look and feel, but is usually SwingConstants.LEFT.
Returns:
The horizontal alignment.

getHorizontalTextPosition

public int getHorizontalTextPosition()
Returns the horizontal position of the label's text relative to the icon. This will be one of SwingConstants.LEFT, SwingConstants.CENTER, SwingConstants.RIGHT, SwingConstants.LEADING and SwingConstants.TRAILING.
Returns:
The horizontal position of the label's text relative to the icon.

getIcon

public Icon getIcon()
Returns the active icon. The active icon is painted when the label is enabled.
Returns:
The active icon.

getIconTextGap

public int getIconTextGap()
Returns the gap between the icon and the text.
Returns:
The gap between the icon and the text.

getLabelFor

public Component getLabelFor()
Returns the component that this JLabel is providing the label for. This component will typically receive the focus when the label's mnemonic key is activated via the keyboard.
Returns:
The component (possibly null).

getText

public String getText()
Returns the text displayed by the label.
Returns:
The label text (possibly null).

getUI

public LabelUI getUI()
Returns the label's UI delegate.
Returns:
The label's UI delegate.

getUIClassID

public String getUIClassID()
Returns a name to identify which look and feel class will be the UI delegate for this label.
Overrides:
getUIClassID in interface JComponent
Returns:
"LabelUI"

getVerticalAlignment

public int getVerticalAlignment()
Returns the vertical alignment of the label (one of SwingConstants.TOP, SwingConstants.CENTER and SwingConstants.BOTTOM). The default value depends on the installed look and feel, but is usually SwingConstants.CENTER.
Returns:
The vertical alignment.

getVerticalTextPosition

public int getVerticalTextPosition()
Returns the vertical position of the label's text relative to the icon. This will be one of SwingConstants.TOP, SwingConstants.CENTER and SwingConstants.BOTTOM.
Returns:
The vertical position of the label's text relative to the icon.

imageUpdate

public boolean imageUpdate(Image img,
                           int infoflags,
                           int x,
                           int y,
                           int w,
                           int h)
Returns false if the current icon image (current icon will depend on whether the label is enabled) is not equal to the passed in image.
Specified by:
imageUpdate in interface ImageObserver
Overrides:
imageUpdate in interface Component
Parameters:
img - The image to check.
infoflags - The bitwise inclusive OR of ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, and WIDTH
x - The x position
y - The y position
w - The width
h - The height
Returns:
Whether the current icon image is equal to the image given.

paramString

protected String paramString()
Returns a string describing the attributes for the JLabel component, for use in debugging. The return value is guaranteed to be non-null, but the format of the string may vary between implementations.
Overrides:
paramString in interface JComponent
Returns:
A string describing the attributes of the JLabel.

setDisabledIcon

public void setDisabledIcon(Icon newIcon)
Sets the icon displayed when the label is disabled (this is a bound property with the name 'disabledIcon').
Parameters:
newIcon - The disabled icon (null permitted).

setDisplayedMnemonic

public void setDisplayedMnemonic(char mnemonic)
Sets the character that will be the label's mnemonic. If the label is used as a label for another component, the label will give focus to that component when the mnemonic is activated via the keyboard.
Parameters:
mnemonic - The character to use for the mnemonic (this will be converted to the equivalent upper case character).

setDisplayedMnemonic

public void setDisplayedMnemonic(int mnemonic)
Sets the keycode that will be the label's mnemonic (this is a bound property with the name 'displayedMnemonic'). If the label is used as a label for another component, the label will give focus to that component when the mnemonic is activated.
Parameters:
mnemonic - The keycode to use for the mnemonic.
See Also: