Skip to main content

Overview

JLabel extends JavaFX Label with:
  • Integrated JIcon support
  • Icon color customization
  • Method chaining for styles
  • Multiline text support

Constructors

JLabel()
constructor
Creates an empty label.
JLabel(text)
constructor
Creates a label with the specified text.
JLabel(icon)
constructor
Creates a label with only an icon.
JLabel(text, icon)
constructor
Creates a label with both text and icon.

Basic Usage

// Simple text label
JLabel label = new JLabel("User Name");

// Label with icon
JIcon userIcon = new JIcon("user");
JLabel labelWithIcon = new JLabel("Profile", userIcon);

// Method chaining
JLabel styledLabel = new JLabel("Important")
    .withStyle("text-lg", "font-bold", "text-danger");

Icon Support

setIcon(icon)
JLabel
Sets or removes the label’s icon.Returns the label instance for method chaining.
JIcon checkIcon = new JIcon("check");
label.setIcon(checkIcon);

// Remove icon
label.setIcon(null);
setIconColor(cssColor)
JLabel
Sets the color of the icon graphic.Returns the label instance for method chaining.
label.setIconColor("#4f46e5");
label.setIconColor("red");

Styling

withStyle(styleClasses...)
JLabel
Adds one or more CSS style classes to the label.Returns the label instance for method chaining.
label.withStyle("text-lg", "font-bold", "text-primary");
addClass(styleClasses...)
JLabel
Alias for withStyle(). Adds CSS style classes to the label.Returns the label instance for method chaining.

Multiline

setMultiline(multiline)
JLabel
Enables or disables text wrapping.Returns the label instance for method chaining.
label.setMultiline(true);
label.setText("This is a long text that will wrap to multiple lines when the label width is constrained.");

Examples

Label with Icon and Color

JIcon warningIcon = new JIcon("alert-triangle");
JLabel warningLabel = new JLabel("Warning: System Update Required", warningIcon)
    .setIconColor("#f59e0b")
    .withStyle("text-warning", "font-semibold");

Styled Section Header

JLabel header = new JLabel("Account Settings")
    .withStyle("text-2xl", "font-bold", "text-gray-900");

Icon-Only Label

JIcon infoIcon = new JIcon("info");
JLabel iconLabel = new JLabel(infoIcon)
    .setIconColor("#2563eb");

Multiline Label

JLabel description = new JLabel()
    .setText("This is a detailed description that may span multiple lines when displayed in a constrained layout.")
    .setMultiline(true)
    .withStyle("text-sm", "text-muted");

Inherits From

JLabel extends javafx.scene.control.Label, so all standard JavaFX Label methods are available:
  • setText(String) - Set label text
  • getText() - Get label text
  • setTextAlignment(TextAlignment) - Set text alignment
  • setContentDisplay(ContentDisplay) - Control icon/text positioning
  • setGraphic(Node) - Set custom graphic (note: setIcon() is preferred for JIcon)
See JavaFX Label documentation for complete API.

Build docs developers (and LLMs) love