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 swing widget that displays a text message and/or an icon.
See Also:
Serialized Form

Nested Class Summary

protected class
JLabel.AccessibleJLabel
Accessibility support for JLabel.

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)
This method ensures that the key is valid as a horizontal alignment.
protected int
checkVerticalKey(int key, String message)
This method ensures that the key is valid as a vertical alignment.
AccessibleContext
getAccessibleContext()
DOCUMENT ME!
Icon
getDisabledIcon()
This method returns the disabled icon.
int
getDisplayedMnemonic()
This method returns the keycode that is used for the label's mnemonic.
int
getDisplayedMnemonicIndex()
This method returns which character in the text will be the underlined character.
int
getHorizontalAlignment()
This method returns the horziontal alignment of the label.
int
getHorizontalTextPosition()
This method returns the horizontal text position of the label.
Icon
getIcon()
This method returns the active icon.
int
getIconTextGap()
This method returns the gap between the icon and the text.
Component
getLabelFor()
This method returns the component that the label gives focus to when the mnemonic is activated.
String
getText()
This method returns the label text.
LabelUI
getUI()
This method returns the label's UI delegate.
String
getUIClassID()
This method returns a name to identify which look and feel class will be the UI delegate for this label.
int
getVerticalAlignment()
This method returns the vertical alignment of the label.
int
getVerticalTextPosition()
This method returns the vertical text position of the label.
boolean
imageUpdate(Image img, int infoflags, int x, int y, int w, int h)
This method simply 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()
This method is used primarily for debugging purposes and returns a string that can be used to represent this label.
void
setDisabledIcon(Icon newIcon)
This method changes the "disabledIcon" property.
void
setDisplayedMnemonic(char mnemonic)
This method sets the character that will be the mnemonic used.
void
setDisplayedMnemonic(int mnemonic)
This method sets the keycode that will be the label's mnemonic.
void
setDisplayedMnemonicIndex(int newIndex)
This method sets which character in the text will be the underlined character.
void
setFont(Font f)
This method overrides setFont so that we can call for a repaint after the font is changed.
void
setHorizontalAlignment(int alignment)
This method changes the "horizontalAlignment" property.
void
setHorizontalTextPosition(int textPosition)
This method changes the "horizontalTextPosition" property of the label.
void
setIcon(Icon newIcon)
This method changes the "icon" property.
void
setIconTextGap(int newGap)
This method changes the "iconTextGap" property.
void
setLabelFor(Component c)
This method changes the "labelFor" property.
void
setText(String newText)
This method changes the "text" property.
void
setUI(LabelUI ui)
This method sets the label's UI delegate.
void
setVerticalAlignment(int alignment)
This method changes the "verticalAlignment" property of the label.
void
setVerticalTextPosition(int textPosition)
This method changes the "verticalTextPosition" property of the label.
void
updateUI()
This method 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, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, 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, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFont, setLayout, swapComponents, 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, 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, getListeners, 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, isOpaque, 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, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, 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.

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.
horizontalAlignment - The horizontal alignment of the label.

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.
icon - The icon to use with the label.
horizontalAlignment - The horizontal alignment of the label.

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.

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.
horizontalAlignment - The horizontal alignment of the label.

Method Details

checkHorizontalKey

protected int checkHorizontalKey(int key,
                                 String message)
This method ensures that the key is valid as a horizontal alignment. Valid keys are: LEFT, CENTER, RIGHT, LEADING, 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's valid.
Throws:
IllegalArgumentException - If the key is invalid.

checkVerticalKey

protected int checkVerticalKey(int key,
                               String message)
This method ensures that the key is valid as a vertical alignment. Valid keys are: TOP, CENTER, and 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's valid.
Throws:
IllegalArgumentException - If the key is invalid.

getAccessibleContext

public AccessibleContext getAccessibleContext()
DOCUMENT ME!
Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in interface JComponent
Returns:
The accessible context.

getDisabledIcon

public Icon getDisabledIcon()
This method 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()
This method 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()
This method returns which character in the text will be the underlined character.
Returns:
The index of the character that will be underlined.

getHorizontalAlignment

public int getHorizontalAlignment()
This method returns the horziontal alignment of the label.
Returns:
The horizontal alignment of the label.

getHorizontalTextPosition

public int getHorizontalTextPosition()
This method returns the horizontal text position of the label.
Returns:
The horizontal text position.

getIcon

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

getIconTextGap

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

getLabelFor

public Component getLabelFor()
This method returns the component that the label gives focus to when the mnemonic is activated.
Returns:
The component that gets focus when the label's mnemonic is activated.

getText

public String getText()
This method returns the label text.
Returns:
The label text.

getUI

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

getUIClassID

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

getVerticalAlignment

public int getVerticalAlignment()
This method returns the vertical alignment of the label.
Returns:
The vertical alignment of the label.

getVerticalTextPosition

public int getVerticalTextPosition()
This method returns the vertical text position of the label.
Returns:
The vertical text position of the label.

imageUpdate

public boolean imageUpdate(Image img,
                           int infoflags,
                           int x,
                           int y,
                           int w,
                           int h)
This method simply 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()
This method is used primarily for debugging purposes and returns a string that can be used to represent this label.
Overrides:
paramString in interface JComponent
Returns:
A string to represent this label.

setDisabledIcon

public void setDisabledIcon(Icon newIcon)
This method changes the "disabledIcon" property. This icon (the disabled icon) will be the one displayed when the label is disabled.
Parameters:
newIcon - The disabled icon.

setDisplayedMnemonic

public void setDisplayedMnemonic(char mnemonic)
This method sets the character that will be the mnemonic used. 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 character to use for the mnemonic.

setDisplayedMnemonic

public void setDisplayedMnemonic(int mnemonic)
This method sets the keycode 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.
Parameters:
mnemonic - The keycode to use for the mnemonic.

setDisplayedMnemonicIndex

public void setDisplayedMnemonicIndex(int newIndex)
            throws IllegalArgumentException
This method sets which character in the text will be the underlined character. If the given index is -1, then this indicates that there is no mnemonic. If the index is less than -1 or if the index is equal to the length, this method will throw an IllegalArgumentException.
Parameters:
newIndex - The index of the character to underline.
Throws:
IllegalArgumentException - If index less than -1 or index equals length.

setFont

public void setFont(Font f)
This method overrides setFont so that we can call for a repaint after the font is changed.
Overrides:
setFont in interface JComponent
Parameters:
f - The font for this label.

setHorizontalAlignment

public void setHorizontalAlignment(int alignment)
This method changes the "horizontalAlignment" property. The horizontal alignment determines where the label will be placed horizontally.
Parameters:
alignment - The horizontal alignment of the label.

setHorizontalTextPosition

public void setHorizontalTextPosition(int textPosition)
This method changes the "horizontalTextPosition" property of the label. The horizontal text position determines where the text will be placed horizontally relative to the icon.
Parameters:
textPosition - The horizontal text position.

setIcon

public void setIcon(Icon newIcon)
This method changes the "icon" property. This icon (the active icon) will be the one displayed when the label is enabled.
Parameters:
newIcon - The active icon.

setIconTextGap

public void setIconTextGap(int newGap)
This method changes the "iconTextGap" property. The iconTextGap determines how much space there is between the icon and the text.
Parameters:
newGap - The gap between the icon and the text.

setLabelFor

public void setLabelFor(Component c)
This method changes the "labelFor" property. The component that the label is acting as a label for will request focus when the label's mnemonic is activated.
Parameters:
c - The component that gets focus when the label's mnemonic is activated.

setText

public void setText(String newText)
This method changes the "text" property. The given text will be painted in the label.
Parameters:
newText - The label's text.

setUI

public void setUI(LabelUI ui)
This method sets the label's UI delegate.
Parameters:
ui - The label's UI delegate.

setVerticalAlignment

public void setVerticalAlignment(int alignment)
This method changes the "verticalAlignment" property of the label. The vertical alignment determines how where the label will be placed vertically. If the alignment is not valid, it will default to the center.
Parameters:
alignment - The vertical alignment of the label.

setVerticalTextPosition

public void setVerticalTextPosition(int textPosition)
This method changes the "verticalTextPosition" property of the label. The vertical text position determines where the text will be placed vertically relative to the icon.
Parameters:
textPosition - The vertical text position.

updateUI

public void updateUI()
This method resets the label's UI delegate to the default UI for the current look and feel.
Overrides:
updateUI in interface JComponent

JLabel.java -- Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.