All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.sun.java.swing.JLabel

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----com.sun.java.swing.JComponent
                           |
                           +----com.sun.java.swing.JLabel

public class JLabel
extends JComponent
implements SwingConstants, Accessible
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 left-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 to the right 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.


Variable Index

 o labelFor
--- Accessibility Support ---

Constructor Index

 o JLabel()
Creates a JLabel instance with no image and with an empty string for the title.
 o JLabel(Icon)
Creates a JLabel instance with the specified image.
 o JLabel(Icon, int)
Creates a JLabel instance with the specified image and horizontal alignment.
 o JLabel(String)
Creates a JLabel instance with the specified text.
 o JLabel(String, Icon, int)
Creates a JLabel instance with the specified text, image, and horizontal alignment.
 o JLabel(String, int)
Creates a JLabel instance with the specified text and horizontal alignment.

Method Index

 o checkHorizontalKey(int, String)
 o checkVerticalKey(int, String)
 o getAccessibleName()
Get the accessible name of this object.
 o getAccessibleRole()
Get the role of this object.
 o 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.
 o getDisplayedKeyAccelerator()
Return the char that indicates a shortcut key.
 o getHorizontalAlignment()
Returns the alignment of the label's contents along the X axis.
 o getHorizontalTextPosition()
Returns the horizontal position of the label's text, relative to its image.
 o getIcon()
Returns the graphic image (glyph, icon) that the label displays.
 o getIconTextGap()
Returns the amount of space between the text and the icon displayed in this label.
 o getLabelFor()
Get the component this is labelling.
 o getText()
Returns the text string that the label displays.
 o getUI()
Return the current L&F for this component.
 o getUIClassID()
Returns a string that specifies the name of the l&f class that renders this component.
 o getVerticalAlignment()
Returns the alignment of the label's contents along the Y axis.
 o getVerticalTextPosition()
Returns the vertical position of the label's text, relative to its image.
 o isOpaque()
Identifies whether the label's background is opaque or transparent.
 o setDisabledIcon(Icon)
Set the icon to be displayed if this JLabel is "disabled", i.e.
 o setDisplayedKeyAccelerator(char)
Specify a character that indicates a shortcut key.
 o setFont(Font)
Sets the font used to display the label's text.
 o setHorizontalAlignment(int)
Sets the alignment of the label's contents along the X axis.
 o setHorizontalTextPosition(int)
Sets the horizontal position of the label's text, relative to its image.
 o setIcon(Icon)
Defines the icon this component will display.
 o setIconTextGap(int)
If both the icon and text properties are set, this property defines the space between them.
 o setLabelFor(Component)
Set the component this is labelling.
 o setOpaque(boolean)
If true the labels background will be filled with the background color.
 o setText(String)
Defines the single line of text this component will display.
 o setUI(LabelUI)
Set the L&F for the component.
 o setVerticalAlignment(int)
Sets the alignment of the label's contents along the Y axis.
 o setVerticalTextPosition(int)
Sets the vertical position of the label's text, relative to its image.
 o updateUI()
Notification from the UIFactory that the L&F has changed.

Variables

 o labelFor
 protected Component labelFor
--- Accessibility Support ---

Constructors

 o 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 to the right of the image.

Parameters:
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, or RIGHT.
 o 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:
text - The text to be displayed by the label.
horizontalAlignment - One of the following constants defined in SwingConstants: LEFT, CENTER, or RIGHT.
 o JLabel
 public JLabel(String text)
Creates a JLabel instance with the specified text. The label is aligned against the left side of its display area, and centered vertically.

Parameters:
text - The text to be displayed by the label.
 o 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:
icon - The image to be displayed by the label.
horizontalAlignment - One of the following constants defined in SwingConstants: LEFT, CENTER, or RIGHT.
 o 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:
icon - The image to be displayed by the label.
 o 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 at the left of the label's display area.

Methods

 o getUI
 public LabelUI getUI()
Return the current L&F for this component.

Returns:
LabelUI object
 o setUI
 public void setUI(LabelUI ui)
Set the L&F for the component.

 o updateUI
 public void updateUI()
Notification from the UIFactory that the L&F has changed.

Overrides:
updateUI in class JComponent
See Also:
updateUI
 o getUIClassID
 public String getUIClassID()
Returns a string that specifies the name of the l&f class that renders this component.

Returns:
String "LabelUI"
Overrides:
getUIClassID in class JComponent
See Also:
getUIClassID, getUI
 o getText
 public String getText()
Returns the text string that the label displays.

Returns:
a String
See Also:
setText
 o 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.

See Also:
setVerticalTextPosition, setHorizontalTextPosition, setIcon
 o getIcon
 public Icon getIcon()
Returns the graphic image (glyph, icon) that the label displays.

Returns:
an Icon
See Also:
setIcon
 o 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.

See Also:
setVerticalTextPosition, setHorizontalTextPosition, getIcon
 o getDisabledIcon
 public 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.

Returns:
The value of the disabledIcon property.
See Also:
setDisabledIcon, ImageIcon
 o setDisabledIcon
 public void setDisabledIcon(Icon disabledIcon)
Set the icon to be displayed if this JLabel is "disabled", i.e. JLabel.setEnabled(false).

The default value of this property is null.

This is a JavaBeans bound property.

See Also:
getDisabledIcon, setEnabled
 o setDisplayedKeyAccelerator
 public void setDisplayedKeyAccelerator(char aKey)
Specify a character that indicates a shortcut key. This property is used when the label is part of a larger component.

 o getDisplayedKeyAccelerator
 public char getDisplayedKeyAccelerator()
Return the char that indicates a shortcut key. This property is used when the label is part of a larger component.

Returns:
char
 o setOpaque
 public void setOpaque(boolean isOpaque)
If true the labels background will be filled with the background color. Otherwise, the background is transparent, and whatever is "underneath" (already drawn where the label is to be drawn) will remain visible in the label's background.

The default value of this property is false.

This is a JavaBeans bound property.

See Also:
isOpaque, getBackground
 o isOpaque
 public boolean isOpaque()
Identifies whether the label's background is opaque or transparent.

Returns:
true if the label's background will be filled with the background color, otherwise false.
Overrides:
isOpaque in class JComponent
See Also:
setOpaque
 o checkHorizontalKey
 protected int checkHorizontalKey(int x,
                                  String s)
 o checkVerticalKey
 protected int checkVerticalKey(int x,
                                String s)
 o getIconTextGap
 public int getIconTextGap()
Returns the amount of space between the text and the icon displayed in this label.

Returns:
an int equal to the number of pixels between the text and the icon.
See Also:
setIconTextGap.
 o 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.

See Also:
getIconTextGap
 o getVerticalAlignment
 public int getVerticalAlignment()
Returns the alignment of the label's contents along the Y axis.

Returns:
The value of the verticalAlignment property, one of the following constants defined in SwingConstants: TOP, CENTER, or BOTTOM.
See Also:
SwingConstants, setVerticalAlignment
 o 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.

This is a JavaBeans bound property.

Parameters:
alignment - One of the following constants defined in SwingConstants: TOP, CENTER (the default), or BOTTOM.
See Also:
SwingConstants, getVerticalAlignment
 o getHorizontalAlignment
 public int getHorizontalAlignment()
Returns the alignment of the label's contents along the X axis.

Returns:
The value of the horizontalAlignment property, one of the following constants defined in SwingConstants: LEFT, CENTER, or RIGHT.
See Also:
setHorizontalAlignment, SwingConstants
 o 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:
alignment - One of the following constants defined in SwingConstants: LEFT (the default for text-only labels), CENTER (the default for image-only labels), or RIGHT.
See Also:
SwingConstants, getHorizontalAlignment.
 o getVerticalTextPosition
 public int getVerticalTextPosition()
Returns the vertical position of the label's text, relative to its image.

Returns:
One of the following constants defined in SwingConstants: TOP, CENTER, or BOTTOM.
See Also:
setVerticalTextPosition, SwingConstants
 o 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:
textPosition - One of the following constants defined in SwingConstants: TOP, CENTER (the default), or BOTTOM.
See Also:
SwingConstants, getVerticalTextPosition
 o getHorizontalTextPosition
 public int getHorizontalTextPosition()
Returns the horizontal position of the label's text, relative to its image.

Returns:
One of the following constants defined in SwingConstants: LEFT, CENTER, or RIGHT.
See Also:
SwingConstants
 o setHorizontalTextPosition
 public void setHorizontalTextPosition(int x)
Sets the horizontal position of the label's text, relative to its image.

Parameters:
x - One of the following constants defined in SwingConstants: LEFT, CENTER, or RIGHT (the default).
See Also:
SwingConstants
 o setFont
 public void setFont(Font font)
Sets the font used to display the label's text.

Parameters:
font - The font to use.
Overrides:
setFont in class Component
 o getAccessibleName
 public String getAccessibleName()
Get the accessible name of this object.

Returns:
the localized name of the object -- can be null if this object does not have a name
Overrides:
getAccessibleName in class JComponent
See Also:
setAccessibleName
 o getAccessibleRole
 public AccessibleRole getAccessibleRole()
Get the role of this object.

Returns:
an instance of AccessibleRole describing the role of the object
Overrides:
getAccessibleRole in class JComponent
See Also:
AccessibleRole
 o getLabelFor
 public Component getLabelFor()
Get the component this is labelling.

Returns:
the Component this is labelling. Can be null if this does not label a Component.
 o setLabelFor
 public void setLabelFor(Component c)
Set the component this is labelling. Can be null if this does not label a Component.


All Packages  Class Hierarchy  This Package  Previous  Next  Index