Class ToggleButton

All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasAriaLabel, HasComponents, HasElement, HasEnabled, HasLabel, HasSize, HasStyle, HasTheme, HasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>,Boolean>, HasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>,Boolean>, HasThemeVariant<ToggleButtonVariant>, HasTooltip, Serializable

@Tag("fc-toggle-button") @JsModule("./fc-toggle-button.js") public class ToggleButton extends AbstractSinglePropertyField<ToggleButton,Boolean> implements HasSize, HasComponents, HasLabel, HasAriaLabel, HasTooltip, HasThemeVariant<ToggleButtonVariant>
A toggle button component built on Vaadin with support for customizable labels and icons.

The component displays a switch with optional left and right labels or icons, and fires a value-change event when toggled.

Since:
1.0.0
See Also:
  • Constructor Details

    • ToggleButton

      public ToggleButton()
      Creates a new toggle button with an initial value of false.
    • ToggleButton

      public ToggleButton(boolean initialValue)
      Creates a new toggle button with the given initial value.
      Parameters:
      initialValue - the initial checked state
      Since:
      1.0.0
    • ToggleButton

      public ToggleButton(String label)
      Creates a new toggle button with the given label and an initial value of false.
      Parameters:
      label - the label text shown above the toggle
      Since:
      1.0.0
    • ToggleButton

      public ToggleButton(String label, boolean initialValue)
      Creates a new toggle button with the given label and initial value.
      Parameters:
      label - the label text shown above the toggle
      initialValue - the initial checked state
      Since:
      1.0.0
  • Method Details

    • setReadOnly

      public void setReadOnly(boolean readOnly)
      Specified by:
      setReadOnly in interface HasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>,Boolean>
      Specified by:
      setReadOnly in interface HasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>,Boolean>
    • isReadOnly

      public boolean isReadOnly()
      Specified by:
      isReadOnly in interface HasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>,Boolean>
      Specified by:
      isReadOnly in interface HasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>,Boolean>
    • setTooltipText

      public Tooltip setTooltipText(String text)
      Specified by:
      setTooltipText in interface HasTooltip
    • getTooltip

      public Tooltip getTooltip()
      Specified by:
      getTooltip in interface HasTooltip
    • setItemLabelGenerator

      public ToggleButton setItemLabelGenerator(ItemLabelGenerator<Boolean> itemLabelGenerator)
      Sets a generator that provides labels for the checked (true) and unchecked (false) states. The generator is called with the state value and its result is used as the right label for true and the left label for false.
      Parameters:
      itemLabelGenerator - the label generator; must not be null
      Returns:
      this instance for method chaining
      Since:
      1.0.0
    • withHighlightLabel

      public ToggleButton withHighlightLabel()
      Enables label highlighting: the label on the active side is shown using the color of the active theme variant (PRIMARY, SUCCESS, WARNING, ERROR, or CONTRAST), falling back to the primary color when no color variant is set. The inactive-side label is dimmed.
      Returns:
      this instance for method chaining
      Since:
      1.0.0
    • withIconsInside

      public ToggleButton withIconsInside()
      Places icons adjacent to the switch and labels on the outer edges, producing the layout [left-label] [left-icon] [switch] [right-icon] [right-label].

      By default the order is [left-icon] [left-label] [switch] [right-label] [right-icon].

      Returns:
      this instance for method chaining
      Since:
      1.0.0
    • withIconsOutside

      public ToggleButton withIconsOutside()
      Restores the default layout where icons are on the outer edges and labels are adjacent to the switch: [left-icon] [left-label] [switch] [right-label] [right-icon].
      Returns:
      this instance for method chaining
      Since:
      1.0.0
    • withoutHighlightLabel

      public ToggleButton withoutHighlightLabel()
      Disables label highlighting so both labels are rendered with the same color regardless of the toggle state.
      Returns:
      this instance for method chaining
      Since:
      1.0.0
    • setLeftLabel

      public ToggleButton setLeftLabel(String label)
      Sets the label displayed on the left side of the toggle switch.
      Parameters:
      label - the left label text
      Returns:
      this instance for method chaining
      Since:
      1.0.0
    • setRightLabel

      public ToggleButton setRightLabel(String label)
      Sets the label displayed on the right side of the toggle switch.
      Parameters:
      label - the right label text
      Returns:
      this instance for method chaining
      Since:
      1.0.0
    • setLeftIcon

      public ToggleButton setLeftIcon(Component icon)
      Sets the icon displayed on the left side of the toggle switch.
      Parameters:
      icon - the component to use as the left icon
      Returns:
      this instance for method chaining
      Since:
      1.0.0
    • setRightIcon

      public ToggleButton setRightIcon(Component icon)
      Sets the icon displayed on the right side of the toggle switch.
      Parameters:
      icon - the component to use as the right icon
      Returns:
      this instance for method chaining
      Since:
      1.0.0