SWING - JLabel Class

The class JLabel can display either text, an image, or both. Label's contents are aligned by setting the vertical and horizontal alignment in its display area. 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.
Following is the declaration for javax.swing.JLabel class −
public class JLabel
   extends JComponent
      implements SwingConstants, Accessible

Following are the fields for javax.swing.JLabel class −
protected Component labelFor
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.
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 icon used by the label when it's disabled.
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 which 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)
Sets 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)
Specifies 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)
Sets 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.
This class inherits methods from the following classes −
javax.swing.JComponent
java.awt.Container
java.awt.Component
java.lang.Object
Create the following Java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >
package com.tutorialspoint.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingControlDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showLabelDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showLabelDemo(){
      headerLabel.setText("Control in action: JLabel");      
      JLabel label  = new JLabel("", JLabel.CENTER);        
      label.setText("Welcome to TutorialsPoint Swing Tutorial.");
      label.setOpaque(true);
      label.setBackground(Color.GRAY);
      label.setForeground(Color.WHITE);
      controlPanel.add(label);
      
      mainFrame.setVisible(true);  
   }
}
Compile the program using the command prompt. Go to D:/ > SWING and type the following command.
D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java

If no error occurs, it means the compilation is successful. Run the program using the following command.
D:\SWING>java com.tutorialspoint.gui.SwingControlDemo

Verify the following output.