The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. It inherits JComponent class.
JLabel class declaration
Let's see the declaration for javax.swing.JLabel class.Commonly used Constructors:
Constructor | Description |
---|---|
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. |
JLabel(String s) | 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: text - The text to be displayed by the label. |
JLabel(Icon image) | public JLabel(Icon image) Creates a JLabel instance with the specified image. The label is centered vertically and horizontally in its display area. Parameters: image - The image to be displayed by the label. |
JLabel(String s, Icon i, int horizontalAlignment) | 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: 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. |
Commonly used Methods:
String getText()
public String getText() Returns the text string that the label displays.Returns: a String
public void setText(String text)
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.
public Icon getIcon()
Returns the graphic image (glyph, icon) that the label displays.Returns: an Icon
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.
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, RIGHT, LEADING or TRAILING.
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: alignment - One of the following constants defined in SwingConstants: TOP, CENTER (the default), or BOTTOM.
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.
Returns: the disabledIcon property
Set the icon to be displayed if this JLabel is "disabled (JLabel.setEnabled(false)).
The default value of this property is null.Parameters:disabledIcon - the Icon to display when the component is disabled
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.Returns:int value for the mnemonic key
public void setDisplayedMnemonic(char aChar)
Specifies the displayedMnemonic as a char value.Parameters:aChar - a char specifying the mnemonic to display
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 propertywhen the mnemonic is activated.
Returns the L&F object that renders this component.
Returns:LabelUI object
Sets the L&F object that renders this component.Parameters:ui - the LabelUI L&F object
public void updateUI()
Resets the UI property to a value from the current look and feel.Overrides:updateUI in class JComponent
|
---|
Java JLabel Example
Output:
Other Methods are
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
, orBOTTOM
.
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.
This is a JavaBeans bound property.
- Parameters:
textPosition
- One of the following constants defined inSwingConstants
:TOP
,CENTER
(the default), orBOTTOM
.
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
,RIGHT
,LEADING
orTRAILING
.
public void setHorizontalTextPosition(int textPosition)
Sets the horizontal position of the label's text, relative to its image.
- Parameters:
textPosition
- One of the following constants defined inSwingConstants
:LEFT
,CENTER
,RIGHT
,LEADING
, orTRAILING
(the default).- Throws:
IllegalArgumentException
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
.- Specified by:
imageUpdate
in interfaceImageObserver
- Overrides:
imageUpdate
in classComponent
- Parameters:
img
- the image being observedinfoflags
- seeimageUpdate
for more informationx
- the x coordinatey
- the y coordinatew
- the widthh
- the height- Returns:
false
if the infoflags indicate that the image is completely loaded;true
otherwise.
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:
paramString
in classJComponent
- Returns:
- a string representation of this JLabel.
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. 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.
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:
c
- the Component this label is for, or null if the label is not the label for a component
public AccessibleContext getAccessibleContext()
Get the AccessibleContext of this object
- Specified by:
getAccessibleContext
in interfaceAccessible
- Overrides:
getAccessibleContext
in classJComponent
- Returns:
- the AccessibleContext of this object
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.
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.
This is a JavaBeans bound property.
No comments:
Post a Comment