Swing JLabel

In this tutorial you will learn about the Swing JLabel and its application with practical example.

Swing JLabel

The JLabel Class is used to display a single line of read only text. Read only means user can not change it but it can be changed by an application. The JLabel class extends JComponent class.
A JLabel can display either text, an image, or both.

Table Of Contents

JLabel Class Declaration

JLabel Constructor

Some commonly used constructor listed below.

JLabel(): This constructor is used to create instance of JLabel with no image and empty string value.

Syntax:-

JLabel(Icon Image): This constructor is used to create instance of JLabel with an Image icon.

Syntax:-

JLabel(String text): This constructor is used to create instance of JLabel with specific text.

Syntax:-

JLabel(String text, Icon image, int horizontalAlignment): This constructor is used to create an instance of JLabel with String ,Image and horizontal alignment.

Syntax:-

JLabel Methos

Some of commonly used methods are listed below.

Icon getIcon(): This method is used to return an icon which is displayed to this Label.

String getText() : This method is used to return String of JLabel.

void setText(String text): This method is used to set Text to the JLabel.

void setIcon(Icon icon): This method is used to set icon on the JLabel.

void setHorizontalAlignment(int alignment): This method is used to sets the horizontal position of the Jlabel’s content along the X- axis.

void setVerticalAlignment(int alignment): This method is used to sets the vertical position of the JLabel’s content along the Y-axis.

int getHorizontalAlignment(): This method is used to returns the alignment of the label’s contents along the X-axis and return type is Integer.

Int getVerticalAlignment(): This method is used to returns the alignment of the label’s contents along with the Y-axis and return type is Integer.

Example :-

From the below example we will get understand about JLabel class. This is the simple example you can do more on it.

Output :-

The output of this code will look like below image.

image001

In this tutorial we have learn about the Swing JLabel and its application with practical example. I hope you will like this tutorial.