Class ToggleButton
java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.AbstractField<C,T>
com.vaadin.flow.component.AbstractSinglePropertyField<ToggleButton,Boolean>
com.flowingcode.vaadin.addons.togglebutton.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:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.vaadin.flow.component.AbstractField
AbstractField.ComponentValueChangeEvent<C extends Component,V extends Object> Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
HasValue.ValueChangeEvent<V extends Object>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>> -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new toggle button with an initial value offalse.ToggleButton(boolean initialValue) Creates a new toggle button with the given initial value.ToggleButton(String label) Creates a new toggle button with the given label and an initial value offalse.ToggleButton(String label, boolean initialValue) Creates a new toggle button with the given label and initial value. -
Method Summary
Modifier and TypeMethodDescriptionbooleansetItemLabelGenerator(ItemLabelGenerator<Boolean> itemLabelGenerator) Sets a generator that provides labels for the checked (true) and unchecked (false) states.setLeftIcon(Component icon) Sets the icon displayed on the left side of the toggle switch.setLeftLabel(String label) Sets the label displayed on the left side of the toggle switch.voidsetReadOnly(boolean readOnly) setRightIcon(Component icon) Sets the icon displayed on the right side of the toggle switch.setRightLabel(String label) Sets the label displayed on the right side of the toggle switch.setTooltipText(String text) Places icons adjacent to the switch and labels on the outer edges, producing the layout[left-label] [left-icon] [switch] [right-icon] [right-label].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].Disables label highlighting so both labels are rendered with the same color regardless of the toggle state.Methods inherited from class com.vaadin.flow.component.AbstractSinglePropertyField
getSynchronizationRegistration, hasValidValue, setPresentationValue, setSynchronizedEventMethods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, getEmptyValue, getValue, isEmpty, setModelValue, setValue, valueEqualsMethods inherited from class com.vaadin.flow.component.Component
addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisibleMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasAriaLabel
getAriaLabel, getAriaLabelledBy, setAriaLabel, setAriaLabelledByMethods inherited from interface com.vaadin.flow.component.HasComponents
add, add, add, addComponentAsFirst, addComponentAtIndex, remove, remove, removeAllMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled, setEnabledMethods inherited from interface com.vaadin.flow.component.HasSize
getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullMethods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeNameMethods inherited from interface com.vaadin.flow.component.shared.HasThemeVariant
addThemeVariants, removeThemeVariantsMethods inherited from interface com.vaadin.flow.component.HasValue
clear, getOptionalValueMethods inherited from interface com.vaadin.flow.component.HasValueAndElement
isRequiredIndicatorVisible, setRequiredIndicatorVisible
-
Constructor Details
-
ToggleButton
public ToggleButton()Creates a new toggle button with an initial value offalse. -
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
Creates a new toggle button with the given label and an initial value offalse.- Parameters:
label- the label text shown above the toggle- Since:
- 1.0.0
-
ToggleButton
Creates a new toggle button with the given label and initial value.- Parameters:
label- the label text shown above the toggleinitialValue- the initial checked state- Since:
- 1.0.0
-
-
Method Details
-
setReadOnly
public void setReadOnly(boolean readOnly) - Specified by:
setReadOnlyin interfaceHasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean> - Specified by:
setReadOnlyin interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean>
-
isReadOnly
public boolean isReadOnly()- Specified by:
isReadOnlyin interfaceHasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean> - Specified by:
isReadOnlyin interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean>
-
setTooltipText
- Specified by:
setTooltipTextin interfaceHasTooltip
-
getTooltip
- Specified by:
getTooltipin interfaceHasTooltip
-
setItemLabelGenerator
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 fortrueand the left label forfalse.- Parameters:
itemLabelGenerator- the label generator; must not benull- Returns:
- this instance for method chaining
- Since:
- 1.0.0
-
withHighlightLabel
Enables label highlighting: the label on the active side is shown using the color of the active theme variant (PRIMARY,SUCCESS,WARNING,ERROR, orCONTRAST), 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
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
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
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
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
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
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
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
-