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.
JLable in Swing:-
JLabel is an important component in Swing that is used to display text, images, or both in the user interface. JLabel does not take input from the user but only displays information.
Features of JLabel:
- Displaying text:
In JLabel you can display a static text, which gives information to the user.
- Displaying Image:
You can also display an image using JLabel.
- Text and Image Together:
In JLabel you can display both text and image together.
- No Auto-Line Breaking:
In JLabel the text does not automatically go to the next line. If the text is long, it has to be scrolled or some other technique is used.
Constructor of JLabel:
- JLabel():
This creates an empty label, which contains neither text nor image.
- JLabel(String text):
This creates a label that contains a text.
- JLabel(Icon icon):
This creates a label that contains an image (Icon).
- JLabel(String text, Icon icon, int alignment):
This creates a label that contains both text and image, and you can also set the position of the text.
//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(“Wel-Come to Swing”);
// JLabel with image
Icon icon = new ImageIcon(“path/to/image.jpg”); // Provied to Image path
JLabel label2 = new JLabel(icon);
// JLabel with both text and image
JLabel label3 = new JLabel(“Image or Text”, icon, JLabel.CENTER);
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
frame.add(label1);
frame.add(label2);
frame.add(label3);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In this example:
- The first JLabel shows only text.
- The second JLabel shows only an image.
- The third JLabel shows both text and an image.
Methods of JLabel:
- setText(String text):
Sets the text to be displayed in the JLabel.
- setIcon(Icon icon):
Sets the image (icon) to be displayed in the JLabel.
- setHorizontalAlignment(int alignment):
Sets the horizontal alignment of the text or image in the JLabel.
Example: JLabel.CENTER or JLabel.LEFT
- setVerticalAlignment(int alignment):
Sets the vertical alignment of the text or image in a JLabel.
- getText():
Gets the text set in a JLabel.
- getIcon():
Gets the image set in a JLabel.
Summary:
JLabel is used to display text and images in Swing.
It is a static component, i.e. no input is taken from the user.
Through JLabel, you can show important information, messages, or images to the user.
JLabel is very easy to use and is essential to make the user interface of Swing attractive.
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