JLabel is a component in Java's Swing library that displays text, an image, or both.
It is commonly used in graphical user interfaces (GUIs) to display static information such as labels for text fields, status messages, or images.
Table of Contents
A JLabel can:
Display a single line of text.
Display an image or icon.
Be aligned horizontally and vertically.
Respond to changes dynamically if linked to other components (though it's not directly interactive).
Basic Structure of a JLabel
To create a label in Swing, the JLabel class is used. It is part of the javax.swing package, so you need to import it.
Basic JLabel Constructor:
JLabel label = new JLabel("Text for Label");
You can add this label to a container, such as a JFrame, to display it on the screen.
1. Creating a Simple JLabel
Let's start with a basic example of creating a JLabel and adding it to a JFrame.
Example 1: Basic JLabel
import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class JLabelExample1 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("JLabel Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 100); JLabel label = new JLabel("Hello, Swing!"); // Create a label with text frame.add(label); // Add the label to the frame frame.setVisible(true); // Make the frame visible }); } }
Explanation:
We create a JFrame and a JLabel with the text “Hello, Swing!”.
The label is added to the frame, and the frame is set visible to display the label.
2. Setting Text and Icons in JLabel
A JLabel can display text, icons, or both. You can create an icon from an image file and set it in a label.
Example 2: JLabel with Text and Icon
import javax.swing.*; import java.awt.*; public class JLabelExample2 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("JLabel with Text and Icon"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 200); // Create an icon from an image Icon icon = new ImageIcon("path_to_image.png"); // Use a valid path to an image // Create a label with text and an icon JLabel label = new JLabel("Hello, Swing with Icon!", icon, JLabel.CENTER); label.setHorizontalTextPosition(JLabel.RIGHT); // Text to the right of the icon label.setVerticalTextPosition(JLabel.CENTER); // Center the text vertically frame.add(label); frame.setVisible(true); }); } }
Explanation:
In this example, we create an Icon using ImageIcon and set it along with the text in the JLabel.
We control the position of the text relative to the icon using setHorizontalTextPosition() and setVerticalTextPosition().
3. Customizing Text and Font in JLabel
You can customize the text's appearance in a JLabel by changing its font, size, and style. This can be done using the setFont() method.
Example 3: Customizing Font in JLabel
import javax.swing.*; import java.awt.*; public class JLabelExample3 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Custom Font JLabel"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 200); JLabel label = new JLabel("Styled Text"); // Set custom font: Font(name, style, size) label.setFont(new Font("Serif", Font.BOLD, 24)); // Set text color label.setForeground(Color.BLUE); frame.add(label); frame.setVisible(true); }); } }
Explanation:
We create a Font object with custom properties (e.g., font name, style, size) and apply it to the label using setFont().
We also change the text color using setForeground() to set a blue color
4. Aligning Text in JLabel
You can control the alignment of the text or icon in a JLabel both horizontally and vertically using setHorizontalAlignment() and setVerticalAlignment().
Example 4: Aligning Text in JLabel
import javax.swing.*; import java.awt.*; public class JLabelExample4 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("JLabel Alignment Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); JLabel label = new JLabel("Aligned Text", JLabel.CENTER); // Center the text horizontally label.setVerticalAlignment(JLabel.TOP); // Align the text to the top frame.add(label); frame.setVisible(true); }); } }
Explanation:
The label's text is centered horizontally using JLabel.CENTER.
The vertical alignment is set to JLabel.TOP, positioning the text at the top of the label.
5. JLabel with HTML Content
JLabel supports simple HTML formatting, allowing you to add basic HTML tags to display styled text.
Example 5: JLabel with HTML
import javax.swing.*; public class JLabelExample5 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("JLabel with HTML Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 200); // Use HTML to style text JLabel label = new JLabel("<html><body style='color:red;'>" + "<h1>Styled Text</h1>" + "<p>This is a paragraph in <b>bold</b> and <i>italic</i>.</p>" + "</body></html>"); frame.add(label); frame.setVisible(true); }); } }
Explanation:
The HTML content is wrapped insideandtags.
6. Updating the Text Dynamically
You can dynamically update the text displayed in a JLabel at runtime using the setText() method.
Example 6: Dynamic Text Update in JLabel
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JLabelExample6 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("Dynamic JLabel Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 150); JLabel label = new JLabel("Initial Text"); JButton button = new JButton("Change Text"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText("Text Updated!"); } }); frame.setLayout(new java.awt.FlowLayout()); frame.add(label); frame.add(button); frame.setVisible(true); }); } }
Explanation:
In this example, the label's text is updated when the button is clicked.
The setText() method allows the text displayed in the label to change dynamically.
7. JLabel with Tooltips
You can add a tooltip to a JLabel using the setToolTipText() method, which will display additional information when the user hovers over the label.
Example 7: Adding a Tooltip to JLabel
import javax.swing.*; public class JLabelExample7 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("JLabel with Tooltip Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 150); JLabel label = new JLabel("Hover over me!"); label.setToolTipText("This is a tooltip that appears when you hover over the label."); frame.add(label); frame.setVisible(true); }); } }
Explanation:
The setToolTipText() method adds a tooltip to the label, which appears when the user hovers over the label.
8. JLabel with Borders
You can add a border around a JLabel to enhance its visual appearance using setBorder().
Example 8: JLabel with a Border
import javax.swing.*; import javax.swing.border.Border; import java.awt.*; public class JLabelExample8 { public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("JLabel with Border Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 150); JLabel label = new JLabel("Label with a border!"); // Create and set a border Border border = BorderFactory.createLineBorder(Color.BLACK, 2); label.setBorder(border); frame.add(label); frame.setVisible(true); }); } }
Explanation:
A black line border is added to the JLabel using BorderFactory.createLineBorder().
The setBorder() method applies the border to the label.
Conclusion
In Java Swing, JLabel is a versatile component that can display text, images, or both, and can be easily customized with fonts, colors, alignment, HTML formatting, and more.
You can also dynamically update the text and even add interactive elements like tooltips. Understanding how to use JLabel effectively allows you to build informative and responsive GUIs.
Summary of Key Points:
Basic Text: Use new JLabel(“text”) to create a label with text.
Icons: Combine text and images using JLabel(icon) or JLabel(text, icon, alignment).
Font Customization: Change font using setFont() and text color using setForeground().
Alignment: Control text and icon alignment using setHorizontalAlignment() and setVerticalAlignment().
Dynamic Updates: Use setText() to change label content dynamically.
HTML Support: Style label text using basic HTML.
Tooltips: Add tooltips using setToolTipText().
Borders: Add a border to a label using setBorder().