SWING - Label

java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JLabel
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(String text)
Creates a JLabel instance with the specified text.
JLabel(String text, Icon icon, int horizontalAlignment)
Creates a JLabel instance with the specified text, image, and horizontal alignment.
JLabel(String text, int horizontalAlignment)
Creates a JLabel instance with the specified text and horizontal alignment.

Method Summary
protected int checkHorizontalKey(int key, String message)
Verify that key is a legal value for the horizontalAlignment properties.
protected int checkVerticalKey(int key, String message)
Verify that key is a legal value for the verticalAlignment or verticalTextPosition properties.
AccessibleContext getAccessibleContext()
Get the AccessibleContext of this object
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.
Component getLabelFor()
Get the component this is labelling.
String getText()
Returns the text string that the label displays.
LabelUI getUI()
Returns the L&F object that renders this component.
String getUIClassID()
Returns a string that specifies the name of the l&f class that renders this component.
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(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.
protected String paramString()
Returns a string representation of this JLabel.
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(Component c)
Set the component this is labelling.
void setText(String text)
Defines the single line of text this component will display.
void setUI(LabelUI ui)
Sets the L&F object that renders this component.
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.
void updateUI()
Resets the UI property to a value from the current look and feel.

Example:

import java.awt.*;
import javax.swing.*;

public class jlabel extends JApplet{
public jlabel() {
Container contentpane=getContentPane();
JLabel label=new JLabel("Hello from Java");
contentpane.setLayout(new FlowLayout()); //Used for Line Alignment
contentpane.add(label);
}
}

The ImageIcon class lets you create an icon from an image file for use in a Swing control.

Constructor Summary
ImageIcon()
Creates an uninitialized image icon.
ImageIcon(byte[] imageData)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF or JPEG.
ImageIcon(byte[] imageData, String description)
Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF or JPEG.
ImageIcon(Image image)
Creates an ImageIcon from an image object.
ImageIcon(Image image, String description)
Creates an ImageIcon from the image.
ImageIcon(String filename)
Creates an ImageIcon from the specified file.
ImageIcon(String filename, String description)
Creates an ImageIcon from the specified file.
ImageIcon(URL location)
Creates an ImageIcon from the specified URL.
ImageIcon(URL location, String description)
Creates an ImageIcon from the specified URL.

Method Summary
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this ImageIcon.
String getDescription()
Gets the description of the image.
int getIconHeight()
Gets the height of the icon.
int getIconWidth()
Gets the width of the icon.
Image getImage()
Returns this icon's Image.
int getImageLoadStatus()
Returns the status of the image loading operation.
ImageObserver getImageObserver()
Returns the image observer for the image.
protected void loadImage(Image image)
Loads the image, returning only when the image is loaded.
void paintIcon(Component c, Graphics g, int x, int y)
Paints the icon.
void setDescription(String description)
Sets the description of the image.
void setImage(Image image)
Sets the image displayed by this icon.
void setImageObserver(ImageObserver observer)
Sets the image observer for the image.
String toString()
Returns a string representation of this image.

Example:

import java.awt.*;
import javax.swing.*;

public class jlabel extends JApplet{

public void jlabel() {
Container contentPane=getContentPane();


JLabel label=new JLabel("Label", new ImageIcon("label.jpg"), JLabel.CENTER);
label.setVerticalTextPosition(JLabel.BOTTOM);
label.setHorizontalTextPosition(JLabel.CENTER);


contentPane.add(label);
}