Class ToggleButton
- All Implemented Interfaces:
AttachNotifier,BlurNotifier<ToggleButton>,DetachNotifier,Focusable<ToggleButton>,FocusNotifier<ToggleButton>,HasAriaLabel,HasElement,HasEnabled,HasHelper,HasLabel,HasSize,HasStyle,HasTheme,HasValidation,HasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,,Boolean>, Boolean> HasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,,Boolean>, Boolean> HasThemeVariant<ToggleButtonVariant>,HasTooltip,HasValidationProperties,Serializable
Switch, with support for customizable
labels and icons on both sides.
The switch itself (value, keyboard, focus, ARIA role and state, read-only and disabled
behavior) is provided by an inner Switch. This component adds the field label, the side
labels and icons, label highlighting, helper text, error message and theme variants around it.
All of them are rendered as regular (light DOM) elements, so the switch can reference them for
accessibility and they can be styled from the application.
The field label, helper text, error message and required indicator are implemented here on
purpose, even though Switch has its own. The ones in Switch render next to the
track, which would place them inside the row, between the side labels and icons. The inner
switch is still marked as required (for aria-required) and invalid, and is linked to
these elements with aria-labelledby and aria-describedby.
- 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> Nested classes/interfaces inherited from interface com.vaadin.flow.component.BlurNotifier
BlurNotifier.BlurEvent<C extends Component>Nested classes/interfaces inherited from interface com.vaadin.flow.component.FocusNotifier
FocusNotifier.FocusEvent<C extends Component>Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
HasValue.ValueChangeEvent<V>, 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 TypeMethodDescriptionbindHelperText(Signal<String> helperText) Binds the helper text to a signal.bindReadOnly(Signal<Boolean> readOnly) bindRequiredIndicatorVisible(Signal<Boolean> requiredIndicatorVisible) voidblur()voidfocus()voidfocus(FocusOption... options) getLabel()intbooleanvoidsetAriaLabel(String ariaLabel) voidsetAriaLabelledBy(String ariaLabelledBy) voidsetErrorMessage(String errorMessage) voidsetHelperComponent(Component component) voidsetHelperText(String helperText) voidsetInvalid(boolean invalid) setItemLabelGenerator(ItemLabelGenerator<Boolean> itemLabelGenerator) Sets a generator that provides labels for the checked (true) and unchecked (false) states.voidsetLeftIcon(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.protected voidsetPresentationValue(Boolean value) voidsetReadOnly(boolean readOnly) voidsetRequiredIndicatorVisible(boolean requiredIndicatorVisible) 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.voidsetTabIndex(int tabIndex) setTooltipText(String text) voidSets the checked state of the toggle.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.AbstractField
addValueChangeListener, bindValue, getEmptyValue, getValue, isEmpty, setModelValue, valueEqualsMethods inherited from class com.vaadin.flow.component.Component
addListener, bindVisible, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onAttach, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisible, whenAttachedMethods 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.Focusable
addFocusShortcutMethods inherited from interface com.vaadin.flow.component.HasAriaLabel
setAriaLabelledByMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods inherited from interface com.vaadin.flow.component.HasEnabled
bindEnabled, isEnabled, setEnabledMethods inherited from interface com.vaadin.flow.component.HasSize
bindHeight, bindWidth, 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, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameMethods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, bindThemeName, bindThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeNameMethods inherited from interface com.vaadin.flow.component.shared.HasThemeVariant
addThemeVariants, bindThemeVariant, bindThemeVariants, removeThemeVariants, setThemeVariant, setThemeVariants, setThemeVariantsMethods inherited from interface com.vaadin.flow.component.shared.HasTooltip
setTooltipMarkdownMethods inherited from interface com.vaadin.flow.component.HasValidation
setManualValidationMethods inherited from interface com.vaadin.flow.component.HasValue
clear, getOptionalValueMethods inherited from interface com.vaadin.flow.component.HasValueAndElement
isReadOnly, isRequiredIndicatorVisible
-
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
-
setValue
Sets the checked state of the toggle.- Specified by:
setValuein interfaceHasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean> - Overrides:
setValuein classAbstractField<ToggleButton,Boolean> - Parameters:
value- the new value; must not benull- Throws:
NullPointerException- ifvalueisnull
-
setPresentationValue
- Specified by:
setPresentationValuein classAbstractField<ToggleButton,Boolean>
-
setReadOnly
public void setReadOnly(boolean readOnly) - Specified by:
setReadOnlyin interfaceHasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean> - Specified by:
setReadOnlyin interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean>
-
setRequiredIndicatorVisible
public void setRequiredIndicatorVisible(boolean requiredIndicatorVisible) - Specified by:
setRequiredIndicatorVisiblein interfaceHasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean> - Specified by:
setRequiredIndicatorVisiblein interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean>
-
bindReadOnly
- Specified by:
bindReadOnlyin interfaceHasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean> - Specified by:
bindReadOnlyin interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean>
-
bindRequiredIndicatorVisible
public SignalBinding<Boolean> bindRequiredIndicatorVisible(Signal<Boolean> requiredIndicatorVisible) - Specified by:
bindRequiredIndicatorVisiblein interfaceHasValue<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean> - Specified by:
bindRequiredIndicatorVisiblein interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<ToggleButton,Boolean>, Boolean>
-
setLabel
-
getLabel
-
setAriaLabel
- Specified by:
setAriaLabelin interfaceHasAriaLabel
-
getAriaLabel
- Specified by:
getAriaLabelin interfaceHasAriaLabel
-
setAriaLabelledBy
- Specified by:
setAriaLabelledByin interfaceHasAriaLabel
-
getAriaLabelledBy
- Specified by:
getAriaLabelledByin interfaceHasAriaLabel
-
focus
public void focus()- Specified by:
focusin interfaceFocusable<ToggleButton>
-
focus
- Specified by:
focusin interfaceFocusable<ToggleButton>
-
blur
public void blur()- Specified by:
blurin interfaceFocusable<ToggleButton>
-
addFocusListener
public Registration addFocusListener(ComponentEventListener<FocusNotifier.FocusEvent<ToggleButton>> listener) - Specified by:
addFocusListenerin interfaceFocusNotifier<ToggleButton>
-
addBlurListener
public Registration addBlurListener(ComponentEventListener<BlurNotifier.BlurEvent<ToggleButton>> listener) - Specified by:
addBlurListenerin interfaceBlurNotifier<ToggleButton>
-
setTabIndex
public void setTabIndex(int tabIndex) - Specified by:
setTabIndexin interfaceFocusable<ToggleButton>
-
getTabIndex
public int getTabIndex()- Specified by:
getTabIndexin interfaceFocusable<ToggleButton>
-
setTooltipText
- Specified by:
setTooltipTextin interfaceHasTooltip
-
getTooltip
- Specified by:
getTooltipin interfaceHasTooltip
-
setHelperText
- Specified by:
setHelperTextin interfaceHasHelper
-
getHelperText
- Specified by:
getHelperTextin interfaceHasHelper
-
bindHelperText
Binds the helper text to a signal. The helper is shown only while the text is not blank, and linked to the switch witharia-describedby.- Specified by:
bindHelperTextin interfaceHasHelper- Parameters:
helperText- the signal to bind the helper text to; must not benull- Returns:
- the signal binding
-
setHelperComponent
- Specified by:
setHelperComponentin interfaceHasHelper
-
getHelperComponent
- Specified by:
getHelperComponentin interfaceHasHelper
-
setErrorMessage
- Specified by:
setErrorMessagein interfaceHasValidation- Specified by:
setErrorMessagein interfaceHasValidationProperties
-
getErrorMessage
- Specified by:
getErrorMessagein interfaceHasValidation- Specified by:
getErrorMessagein interfaceHasValidationProperties
-
setInvalid
public void setInvalid(boolean invalid) - Specified by:
setInvalidin interfaceHasValidation- Specified by:
setInvalidin interfaceHasValidationProperties
-
isInvalid
public boolean isInvalid()- Specified by:
isInvalidin interfaceHasValidation- Specified by:
isInvalidin interfaceHasValidationProperties
-
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
-