|
JSR-209 (Final Release) | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JLabel
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.
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.
JLabel.setDisplayedMnemonic(int)
and
JLabel.setDisplayedMnemonic(char)
to fail
silently if this method can not be supported by the platform. The
following side effects will occur:
java.beans
package.
Please see java.beans.XMLEncoder
.
Field Summary | |
protected java.awt.Component |
labelFor
|
Fields inherited from class javax.swing.JComponent |
listenerList, TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW |
Fields inherited from class java.awt.Component |
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
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 |
Fields inherited from interface java.awt.image.ImageObserver |
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
JLabel()
Creates a JLabel instance with
no image and with an empty string for the title. |
|
JLabel(Icon image)
Creates a JLabel instance with the specified image. |
|
JLabel(Icon image,
int horizontalAlignment)
Creates a JLabel instance with the specified
image and horizontal alignment. |
|
JLabel(java.lang.String text)
Creates a JLabel instance with the specified text. |
|
JLabel(java.lang.String text,
Icon icon,
int horizontalAlignment)
Creates a JLabel instance with the specified
text, image, and horizontal alignment. |
|
JLabel(java.lang.String text,
int horizontalAlignment)
Creates a JLabel instance with the specified
text and horizontal alignment. |
Method Summary | |
protected int |
checkHorizontalKey(int key,
java.lang.String message)
Verify that key is a legal value for the horizontalAlignment properties. |
protected int |
checkVerticalKey(int key,
java.lang.String message)
Verify that key is a legal value for the verticalAlignment or verticalTextPosition properties. |
Icon |
getDisabledIcon()
Returns the value of the disabledIcon property if it's been set, If it hasn't been set and the value of the icon property is an ImageIcon, we compute a "grayed out" version of the icon and update the disabledIcon property with that. |
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. |
java.awt.Component |
getLabelFor()
Get the component this is labelling. |
java.lang.String |
getText()
Returns the text string that the label displays. |
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(java.awt.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 . |
void |
paint(java.awt.Graphics g)
Invoked by Swing to draw components. |
protected void |
paintBorder(java.awt.Graphics g)
Paints the component's border. |
protected void |
paintComponent(java.awt.Graphics g)
Calls the UI delegate's paint method, if the UI delegate is non- null . |
void |
paintComponents(java.awt.Graphics g)
|
protected java.lang.String |
paramString()
Returns a string representation of this JLabel. |
protected void |
printBorder(java.awt.Graphics g)
Prints the component's border. |
protected void |
printComponent(java.awt.Graphics g)
This is invoked during a printing operation. |
void |
printComponents(java.awt.Graphics g)
|
void |
setDisabledIcon(Icon disabledIcon)
Set the icon to be displayed if this JLabel is "disabled" (JLabel.setEnabled(false)). |
void |
setDisplayedMnemonic(char aChar)
Specifies the displayedMnemonic as a char value. |
void |
setDisplayedMnemonic(int key)
Specify a keycode that indicates a mnemonic key. |
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(java.awt.Component c)
Set the component this is labelling. |
void |
setText(java.lang.String text)
Defines the single line of text this component will display. |
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. |
Methods inherited from class java.awt.Container |
add, add, add, add, add, addContainerListener, addImpl, areFocusTraversalKeysSet, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicySet, list, list, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree |
Methods inherited from class java.awt.Component |
addComponentListener, addFocusListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentListeners, getCursor, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getSize, getToolkit, getTreeLock, hasFocus, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isShowing, isValid, isVisible, list, list, list, paintAll, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, repaint, repaint, repaint, setBounds, setBounds, setCursor, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, toString, transferFocus, transferFocusUpCycle |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected java.awt.Component labelFor
Constructor Detail |
public JLabel(java.lang.String text, Icon icon, int horizontalAlignment)
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.
text
- The text to be displayed by the label.icon
- The image to be displayed by the label.horizontalAlignment
- One of the following constants
defined in SwingConstants
:
LEFT
,
CENTER
,
RIGHT
,
LEADING
or
TRAILING
.public JLabel(java.lang.String text, int horizontalAlignment)
JLabel
instance with the specified
text and horizontal alignment.
The label is centered vertically in its display area.
text
- The text to be displayed by the label.horizontalAlignment
- One of the following constants
defined in SwingConstants
:
LEFT
,
CENTER
,
RIGHT
,
LEADING
or
TRAILING
.public JLabel(java.lang.String text)
JLabel
instance with the specified text.
The label is aligned against the leading edge of its display area,
and centered vertically.
text
- The text to be displayed by the label.public JLabel(Icon image, int horizontalAlignment)
JLabel
instance with the specified
image and horizontal alignment.
The label is centered vertically in its display area.
image
- The image to be displayed by the label.horizontalAlignment
- One of the following constants
defined in SwingConstants
:
LEFT
,
CENTER
,
RIGHT
,
LEADING
or
TRAILING
.public JLabel(Icon image)
JLabel
instance with the specified image.
The label is centered vertically and horizontally
in its display area.
image
- The image to be displayed by the label.public JLabel()
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.
Method Detail |
public final void paint(java.awt.Graphics g)
JComponent
paint
directly,
but should instead use the repaint
method to
schedule the component for redrawing.
This method actually delegates the work of painting to three
protected methods: paintComponent
,
paintBorder
,
and paintChildren
. They're called in the order
listed to ensure that children appear on top of component itself.
Generally speaking, the component and its children should not
paint in the insets area allocated to the border. Subclasses can
just override this method, as always.
paint
in class JComponent
g
- the Graphics
context in which to paintJComponent.paintBorder(java.awt.Graphics)
,
JComponent.paintChildren(java.awt.Graphics)
,
JComponent.getComponentGraphics(java.awt.Graphics)
,
JComponent.repaint(long, int, int, int, int)
public java.lang.String getText()
setText(java.lang.String)
public void setText(java.lang.String text)
The default value of this property is null.
This is a JavaBeans bound property.
setVerticalTextPosition(int)
,
setHorizontalTextPosition(int)
,
setIcon(javax.swing.Icon)
public Icon getIcon()
setIcon(javax.swing.Icon)
public void setIcon(Icon icon)
The default value of this property is null.
This is a JavaBeans bound property.
setVerticalTextPosition(int)
,
setHorizontalTextPosition(int)
,
getIcon()
public Icon getDisabledIcon()
setDisabledIcon(javax.swing.Icon)
,
ImageIcon
public void setDisabledIcon(Icon disabledIcon)
The default value of this property is null.
disabledIcon
- the Icon to display when the component is disabledgetDisabledIcon()
,
JComponent.setEnabled(boolean)
public void setDisplayedMnemonic(int key)
getLabelFor()
,
setLabelFor(java.awt.Component)
public void setDisplayedMnemonic(char aChar)
aChar
- a char specifying the mnemonic to displaysetDisplayedMnemonic(int)
public int getDisplayedMnemonic()
getLabelFor()
,
setLabelFor(java.awt.Component)
public void setDisplayedMnemonicIndex(int index) throws java.lang.IllegalArgumentException
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
setMnemonic(KeyEvent.VK_A)
.
index
- Index into the String to underline
java.lang.IllegalArgumentException
- will be thrown if index
= length of the text, or < -1public int getDisplayedMnemonicIndex()
setDisplayedMnemonicIndex(int)
protected int checkHorizontalKey(int key, java.lang.String message)
key
- the property value to checkmessage
- the IllegalArgumentException detail message
java.lang.IllegalArgumentException
- if key isn't LEFT, CENTER, RIGHT,
LEADING or TRAILING.setHorizontalTextPosition(int)
,
setHorizontalAlignment(int)
protected int checkVerticalKey(int key, java.lang.String message)
key
- the property value to checkmessage
- the IllegalArgumentException detail message
java.lang.IllegalArgumentException
- if key isn't TOP, CENTER, or BOTTOM.setVerticalAlignment(int)
,
setVerticalTextPosition(int)
public int getIconTextGap()
setIconTextGap(int)
public void setIconTextGap(int iconTextGap)
The default value of this property is 4 pixels.
This is a JavaBeans bound property.
getIconTextGap()
public int getVerticalAlignment()
SwingConstants
:
TOP
,
CENTER
, or
BOTTOM
.SwingConstants
,
setVerticalAlignment(int)
public void setVerticalAlignment(int alignment)
The default value of this property is CENTER.
alignment
- One of the following constants
defined in SwingConstants
:
TOP
,
CENTER
(the default), or
BOTTOM
.SwingConstants
,
getVerticalAlignment()
public int getHorizontalAlignment()
SwingConstants
:
LEFT
,
CENTER
,
RIGHT
,
LEADING
or
TRAILING
.setHorizontalAlignment(int)
,
SwingConstants
public void setHorizontalAlignment(int alignment)
This is a JavaBeans bound property.
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
.SwingConstants
,
getHorizontalAlignment()
public int getVerticalTextPosition()
SwingConstants
:
TOP
,
CENTER
, or
BOTTOM
.setVerticalTextPosition(int)
,
SwingConstants
public void setVerticalTextPosition(int textPosition)
The default value of this property is CENTER.
This is a JavaBeans bound property.
textPosition
- One of the following constants
defined in SwingConstants
:
TOP
,
CENTER
(the default), or
BOTTOM
.SwingConstants
,
getVerticalTextPosition()
public int getHorizontalTextPosition()
SwingConstants
:
LEFT
,
CENTER
,
RIGHT
,
LEADING
or
TRAILING
.SwingConstants
public void setHorizontalTextPosition(int textPosition)
textPosition
- One of the following constants
defined in SwingConstants
:
LEFT
,
CENTER
,
RIGHT
,
LEADING
, or
TRAILING
(the default).
java.lang.IllegalArgumentException
SwingConstants
public boolean imageUpdate(java.awt.Image img, int infoflags, int x, int y, int w, int h)
img
.
imageUpdate
in interface java.awt.image.ImageObserver
ImageObserver
,
Component.imageUpdate(java.awt.Image, int, int, int, int, int)
protected java.lang.String paramString()
null
.
paramString
in class JComponent
public java.awt.Component getLabelFor()
getDisplayedMnemonic()
,
setDisplayedMnemonic(int)
public void setLabelFor(java.awt.Component c)
c
- the Component this label is for, or null if the label is
not the label for a componentgetDisplayedMnemonic()
,
setDisplayedMnemonic(int)
protected final void paintComponent(java.awt.Graphics g)
JComponent
null
. We pass the delegate a copy of the
Graphics
object to protect the rest of the
paint code from irrevocable changes
(for example, Graphics.translate
).
paintComponent
in class JComponent
g
- the Graphics
object to protectJComponent.paint(java.awt.Graphics)
,
ComponentUI
protected final void paintBorder(java.awt.Graphics g)
JComponent
paintBorder
in class JComponent
g
- the Graphics
context in which to paintJComponent.paint(java.awt.Graphics)
,
JComponent.setBorder(javax.swing.border.Border)
public final void paintComponents(java.awt.Graphics g)
protected final void printComponent(java.awt.Graphics g)
JComponent
paintComponent
on the component.
printComponent
in class JComponent
g
- the Graphics
context in which to paintJComponent.print(java.awt.Graphics)
protected final void printBorder(java.awt.Graphics g)
JComponent
paintBorder
on the component.
printBorder
in class JComponent
g
- the Graphics
context in which to paintJComponent.print(java.awt.Graphics)
public final void printComponents(java.awt.Graphics g)
|
JSR-209 (Final Release) | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |