JLabel and Icons - potatoscript/JavaSwing GitHub Wiki
JLabel
is a GUI component used to display:
- 📝 Text – Static labels, descriptions, or titles.
- 🖼️ Icons – Images and icons.
- 🔄 Combined Text and Icon – Text and icons together.
✅ Key Features:
- Supports HTML formatting in text.
- Can be used as an informational display.
- Aligns text and icons easily.
import javax.swing.*;
public class MyLabel {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("JLabel Example");
// Create JLabel
JLabel label = new JLabel("Hello, Java Swing! 🎉");
// Add label to frame
frame.add(label);
// Set frame properties
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ JLabel label = new JLabel("Hello, Java Swing! 🎉");
- Creates a
JLabel
with the text"Hello, Java Swing! 🎉"
.
✅ frame.add(label);
- Adds the label to the
JFrame
.
✅ frame.setSize(300, 150);
- Sets the window size to 300x150 pixels.
✅ frame.setVisible(true);
- Displays the frame.
+----------------------------+
| Hello, Java Swing! 🎉 |
+----------------------------+
import javax.swing.*;
public class LabelWithIcon {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Label with Icon");
// Load Image Icon
ImageIcon icon = new ImageIcon("icon.png");
// Create JLabel with Icon
JLabel label = new JLabel(icon);
// Add label to frame
frame.add(label);
// Set frame properties
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ ImageIcon icon = new ImageIcon("icon.png");
- Loads an image from the file
icon.png
.
✅ JLabel label = new JLabel(icon);
- Creates a label and attaches the icon.
✅ frame.add(label);
- Adds the label with the icon to the frame.
+-------------------------+
| [Image from icon.png] |
+-------------------------+
import javax.swing.*;
public class LabelWithTextAndIcon {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Label with Text and Icon");
// Load Image Icon
ImageIcon icon = new ImageIcon("icon.png");
// Create JLabel with text and icon
JLabel label = new JLabel("Welcome to Java Swing! 🎨", icon, JLabel.CENTER);
// Add label to frame
frame.add(label);
// Set frame properties
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ JLabel.CENTER
- Aligns the text and icon to the center.
✅ JLabel label = new JLabel("...", icon, JLabel.CENTER);
- Combines text and icon in one label.
+-----------------------------------+
| [icon.png] Welcome to Java Swing!|
+-----------------------------------+
import javax.swing.*;
public class IconPositionExample {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Icon Position Example");
// Load Icon
ImageIcon icon = new ImageIcon("icon.png");
// Create JLabel with text and icon
JLabel label = new JLabel("Icon on the Right →", icon, JLabel.LEFT);
// Set Icon Position
label.setHorizontalTextPosition(JLabel.LEFT);
label.setVerticalTextPosition(JLabel.CENTER);
// Add label to frame
frame.add(label);
// Set frame properties
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ label.setHorizontalTextPosition(JLabel.LEFT);
- Sets the icon to appear right of the text.
✅ label.setVerticalTextPosition(JLabel.CENTER);
- Aligns the text and icon vertically.
+-------------------------------+
| Icon on the Right → [icon.png] |
+-------------------------------+
import javax.swing.*;
public class HtmlLabelExample {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("HTML in JLabel");
// Create JLabel with HTML text
JLabel label = new JLabel("<html><h1 style='color:blue;'>Hello, <i>Java Swing!</i></h1><br/>" +
"<p style='color:green;'>Welcome to GUI programming.</p></html>");
// Add label to frame
frame.add(label);
// Set frame properties
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ HTML Support:
- Swing supports basic HTML tags in
JLabel
using<html>...</html>
syntax.
✅ Formatting Options:
-
<h1>
,<p>
,<b>
,<i>
,<br/>
– Basic HTML tags.
+-------------------------------+
| Hello, Java Swing! |
| Welcome to GUI programming. |
+-------------------------------+
import javax.swing.*;
import java.awt.*;
public class CustomLabel {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Custom JLabel");
// Create JLabel
JLabel label = new JLabel("Styled Label 🎨");
// Set Font
label.setFont(new Font("Arial", Font.BOLD, 18));
// Set Text Color
label.setForeground(Color.MAGENTA);
// Set Background and make it opaque
label.setOpaque(true);
label.setBackground(Color.LIGHT_GRAY);
// Align text center
label.setHorizontalAlignment(JLabel.CENTER);
// Add label to frame
frame.add(label);
// Set frame properties
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ label.setFont(new Font("Arial", Font.BOLD, 18));
- Sets the font, style, and size.
✅ label.setForeground(Color.MAGENTA);
- Sets the text color to magenta.
✅ label.setOpaque(true);
- Makes the label background visible.
✅ label.setBackground(Color.LIGHT_GRAY);
- Sets the background color.
+---------------------+
| Styled Label 🎨 |
+---------------------+
import javax.swing.*;
public class LabelWithTooltip {
public static void main(String[] args) {
// Create JFrame
JFrame frame = new JFrame("Label with Tooltip");
// Create JLabel
JLabel label = new JLabel("Hover over me! 🎈");
// Set Tooltip
label.setToolTipText("This is a tooltip. 🎯");
// Add label to frame
frame.add(label);
// Set frame properties
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
✅ label.setToolTipText("This is a tooltip. 🎯");
- Displays a tooltip when the mouse hovers over the label.
+--------------------------+
| Hover over me! 🎈 |
+--------------------------+
➡️ Hovering:
Tooltip: This is a tooltip. 🎯
Method | Description | Example Code |
---|---|---|
setText(String text) |
Sets the label text. | label.setText("New Text"); |
getText() |
Gets the label text. | String text = label.getText(); |
setIcon(Icon icon) |
Sets an icon on the label. | label.setIcon(new ImageIcon("icon.png")); |
setHorizontalAlignment(int alignment) |
Aligns text horizontally. | label.setHorizontalAlignment(JLabel.CENTER); |
setVerticalAlignment(int alignment) |
Aligns text vertically. | label.setVerticalAlignment(JLabel.TOP); |
setHorizontalTextPosition(int position) |
Sets the icon position horizontally. | label.setHorizontalTextPosition(JLabel.RIGHT); |
setVerticalTextPosition(int position) |
Sets the icon position vertically. | label.setVerticalTextPosition(JLabel.BOTTOM); |
setForeground(Color color) |
Sets the text color. | label.setForeground(Color.RED); |
setBackground(Color color) |
Sets the background color. | label.setBackground(Color.YELLOW); |
setFont(Font font) |
Sets the font and size. | label.setFont(new Font("Arial", Font.BOLD, 16)); |
setToolTipText(String text) |
Adds a tooltip to the label. | label.setToolTipText("Info here"); |
JLabel label = new JLabel("<html>Line 1<br/>Line 2<br/><b>Line 3</b></html>");