In this article JLable in Swing we give the information about it is used to display text, images, or both in the user interface. JLabel does not take input from the user but only displays information.
JLabel in Swing
Introduction
JLabel is an important component in Java Swing that is used to display text, images, or both in a user interface.
It is a non-interactive component, meaning users cannot input data through it — it only displays information.
Features of JLabel
- Displaying Text:
You can display static text to convey information to the user. - Displaying Image:
You can display an image using an Icon object. - Displaying Text and Image Together:
Both text and image can be displayed simultaneously. - No Auto Line Breaking:
Text in a JLabel does not automatically wrap to the next line.
If the text is long, scrolling or custom wrapping methods must be used.
Constructors of JLabel
| Constructor | Description |
| JLabel() | Creates an empty label with no text or image. |
| JLabel(String text) | Creates a label displaying text. |
| JLabel(Icon icon) | Creates a label displaying an image. |
| JLabel(String text, Icon icon, int alignment) | Creates a label with both text and image, with alignment specified. |
Example of JLabel
import javax.swing.*;
public class JLabelExample {
public static void main(String[] args) {
JFrame frame = new JFrame(“JLabel Example”);
// JLabel with text
JLabel label1 = new JLabel(“Welcome to Swing”);
// JLabel with image
Icon icon = new ImageIcon(“path/to/image.jpg”); // Provide image path
JLabel label2 = new JLabel(icon);
// JLabel with both text and image
JLabel label3 = new JLabel(“Image and Text”, icon, JLabel.CENTER);
// Setting layout and adding labels
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(label1);
frame.add(label2);
frame.add(label3);
// Frame settings
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Explanation
- The first JLabel displays only text.
- The second JLabel displays only an image.
- The third JLabel displays both text and an image together.
Common Methods of JLabel
| Method | Description |
| setText(String text) | Sets text to be displayed on the label. |
| getText() | Returns the text of the label. |
| setIcon(Icon icon) | Sets an image (icon) on the label. |
| getIcon() | Returns the icon currently set on the label. |
| setHorizontalAlignment(int alignment) | Sets horizontal alignment (e.g. JLabel.LEFT, JLabel.CENTER, JLabel.RIGHT). |
| setVerticalAlignment(int alignment) | Sets vertical alignment (e.g. JLabel.TOP, JLabel.CENTER, JLabel.BOTTOM). |
Example – Text Alignment
JLabel label = new JLabel(“Aligned Text”);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.TOP);
This positions the text centered horizontally and top-aligned vertically inside the JLabel.
Summary
| Point | Description |
| Component Name | JLabel |
| Purpose | To display static text, images, or both |
| User Input | Not allowed (read-only) |
| Common Uses | Showing messages, captions, or icons |
| Alignment Options | LEFT, RIGHT, CENTER, TOP, BOTTOM |
| Key Advantage | Lightweight, easy to use, enhances UI appearance |
Conclusion
JLabel is a non-interactive Swing component used to present static information like text or images.
It plays a crucial role in making GUIs informative and visually appealing.
By combining text, images, and alignment options, you can design clear and attractive interfaces.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining