Skip to main content

Overview

JSwitch is a toggle switch component that extends JCheckBox and provides an iOS-style switch appearance. It inherits all properties and methods from JCheckBox.

Constructors

JSwitch()

Creates a switch with no text label.
JSwitch switchControl = new JSwitch();

JSwitch(String text)

Creates a switch with the specified text label.
text
String
The text label to display next to the switch
JSwitch switchControl = new JSwitch("Dark mode");

Inherited Properties

Since JSwitch extends JCheckBox, it inherits all properties:

colorStyle

colorStyle
CheckBoxStyle
default:"PRIMARY"
The color style of the switch
Available Values:
  • PRIMARY - Primary theme color
  • SUCCESS - Success/green color
  • DANGER - Danger/red color
  • WARNING - Warning/yellow color
  • INFO - Info/blue color
  • DARK - Dark color
  • SECONDARY - Secondary theme color

size

size
CheckBoxSize
default:"DEFAULT"
The size of the switch
Available Values:
  • SMALL - Small switch
  • DEFAULT - Default/medium switch
  • LARGE - Large switch

Inherited Methods

All methods from JCheckBox are available:

setColorStyle(CheckBoxStyle style)

Sets the color style of the switch.
style
JCheckBox.CheckBoxStyle
required
The color style to apply
switchControl.setColorStyle(JCheckBox.CheckBoxStyle.SUCCESS);

getColorStyle()

Returns the current color style.
colorStyle
JCheckBox.CheckBoxStyle
The current color style of the switch

colorStyleProperty()

Returns the color style property for binding.
colorStyleProperty
ObjectProperty<JCheckBox.CheckBoxStyle>
The observable property for color style

setSize(CheckBoxSize size)

Sets the size of the switch.
size
JCheckBox.CheckBoxSize
required
The size to apply
switchControl.setSize(JCheckBox.CheckBoxSize.LARGE);

getSize()

Returns the current size.
size
JCheckBox.CheckBoxSize
The current size of the switch

sizeProperty()

Returns the size property for binding.
sizeProperty
ObjectProperty<JCheckBox.CheckBoxSize>
The observable property for size

Example Usage

import com.jjarroyo.components.JSwitch;
import com.jjarroyo.components.JCheckBox;

// Create a switch with custom style
JSwitch darkModeSwitch = new JSwitch("Dark Mode");
darkModeSwitch.setColorStyle(JCheckBox.CheckBoxStyle.PRIMARY);
darkModeSwitch.setSize(JCheckBox.CheckBoxSize.DEFAULT);

// Listen to toggle changes
darkModeSwitch.selectedProperty().addListener((obs, wasSelected, isSelected) -> {
    if (isSelected) {
        System.out.println("Dark mode enabled");
        // Apply dark theme
    } else {
        System.out.println("Dark mode disabled");
        // Apply light theme
    }
});

// Set initial state
darkModeSwitch.setSelected(false);

// Bind to another property
BooleanProperty darkModeEnabled = new SimpleBooleanProperty();
darkModeSwitch.selectedProperty().bindBidirectional(darkModeEnabled);

CSS Style Classes

The component applies the following style classes:
  • j-switch - Switch-specific style class
  • j-checkbox - Inherited base style class
  • checkbox-primary, checkbox-success, etc. - Inherited color style classes
  • checkbox-sm, checkbox-md, checkbox-lg - Inherited size style classes

Design Notes

JSwitch provides an iOS-style toggle switch appearance while maintaining all the functionality of a checkbox. The visual design differentiates it from a standard checkbox with rounded track and animated toggle behavior.

Build docs developers (and LLMs) love