Class Switch
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier,com.vaadin.flow.component.BlurNotifier<Switch>,com.vaadin.flow.component.ClickNotifier<Switch>,com.vaadin.flow.component.DetachNotifier,com.vaadin.flow.component.Focusable<Switch>,com.vaadin.flow.component.FocusNotifier<Switch>,com.vaadin.flow.component.HasAriaLabel,com.vaadin.flow.component.HasElement,com.vaadin.flow.component.HasEnabled,com.vaadin.flow.component.HasHelper,com.vaadin.flow.component.HasLabel,com.vaadin.flow.component.HasSize,com.vaadin.flow.component.HasStyle,com.vaadin.flow.component.HasTheme,com.vaadin.flow.component.HasValidation,com.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch,,Boolean>, Boolean> com.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch,,Boolean>, Boolean> com.vaadin.flow.component.shared.HasThemeVariant<SwitchVariant>,com.vaadin.flow.component.shared.HasTooltip,com.vaadin.flow.component.shared.HasValidationProperties,com.vaadin.flow.component.shared.InputField<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch,,Boolean>, Boolean> com.vaadin.flow.data.binder.HasValidator<Boolean>,Serializable
This component is experimental and needs to be enabled with the
com.vaadin.experimental.switchComponent feature flag.
Switch is functionally equivalent to Checkbox, but it is presented as
an on/off toggle and is intended for a single setting that takes effect
immediately. Unlike Checkbox, Switch has no indeterminate state.
Validation
Switch comes with a built-in validation mechanism that verifies that the
field is selected when required
is enabled.
Validation is triggered whenever the user toggles the switch. Programmatic toggling triggers validation as well. If validation fails, the component is marked as invalid and an error message is displayed below the input.
The required error message can be configured using either
Switch.SwitchI18n.setRequiredErrorMessage(String) or
HasValidationProperties.setErrorMessage(String).
For more advanced validation that requires custom rules, you can use
Binder. Please note that Binder provides its own API for the required
validation, see asRequired().
However, if Binder doesn't fit your needs and you want to implement fully
custom validation logic, you can disable the built-in validation by setting
setManualValidation(boolean) to true. This will allow you to control
the invalid state and the error message manually using
HasValidationProperties.setInvalid(boolean) and HasValidationProperties.setErrorMessage(String) API.
- Since:
- 25.3
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.vaadin.flow.component.AbstractField
com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<C extends com.vaadin.flow.component.Component,V> Nested classes/interfaces inherited from interface com.vaadin.flow.component.BlurNotifier
com.vaadin.flow.component.BlurNotifier.BlurEvent<C extends com.vaadin.flow.component.Component>Nested classes/interfaces inherited from interface com.vaadin.flow.component.FocusNotifier
com.vaadin.flow.component.FocusNotifier.FocusEvent<C extends com.vaadin.flow.component.Component>Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
com.vaadin.flow.component.HasValue.ValueChangeEvent<V>, com.vaadin.flow.component.HasValue.ValueChangeListener<E extends com.vaadin.flow.component.HasValue.ValueChangeEvent<?>> -
Constructor Summary
ConstructorsConstructorDescriptionSwitch()Default constructor.Switch(boolean initialValue) Constructs a switch with the initial value.Switch(boolean initialValue, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch, Boolean>> listener) Constructs a switch with the initial value and value change listener.Constructs a switch with the initial label text.Constructs a switch with the initial label text and value.Switch(String labelText, boolean initialValue, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch, Boolean>> listener) Constructs a switch with the initial value, label text and value change listener.Switch(String label, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch, Boolean>> listener) Constructs a switch with the initial label text and value change listener. -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.data.binder.Validator<Boolean> getI18n()Gets the internationalization object previously set for this component.getLabel()Get the current label text.booleanGet the state for the auto-focus property of the switch.booleanGets whether the user is required to switch on the control.protected voidonAttach(com.vaadin.flow.component.AttachEvent attachEvent) voidsetAriaLabel(String ariaLabel) voidsetAriaLabelledBy(String ariaLabelledBy) voidsetAutofocus(boolean autofocus) Set the switch to be input focused when the page loads.voidsetI18n(Switch.SwitchI18n i18n) Sets the internationalization object for this component.voidSet the current label text of this switch.voidsetLabelComponent(com.vaadin.flow.component.Component component) Replaces the label content with the given label component.voidsetManualValidation(boolean enabled) voidsetRequiredIndicatorVisible(boolean required) Sets whether the user is required to switch on the control.protected voidvalidate()Validates the current value against the constraints and sets theinvalidproperty and theerrorMessageproperty based on the result.Methods inherited from class com.vaadin.flow.component.AbstractSinglePropertyField
getSynchronizationRegistration, hasValidValue, setPresentationValue, setSynchronizedEventMethods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, bindValue, getEmptyValue, getValue, isEmpty, setModelValue, setValue, 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, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, 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.BlurNotifier
addBlurListenerMethods inherited from interface com.vaadin.flow.component.ClickNotifier
addClickListener, addClickShortcut, addDoubleClickListener, addSingleClickListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.Focusable
addFocusShortcut, blur, focus, focus, getTabIndex, setTabIndexMethods inherited from interface com.vaadin.flow.component.FocusNotifier
addFocusListenerMethods 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.HasHelper
bindHelperText, getHelperComponent, getHelperText, setHelperComponent, setHelperTextMethods 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
getTooltip, setTooltipMarkdown, setTooltipTextMethods inherited from interface com.vaadin.flow.component.shared.HasValidationProperties
getErrorMessage, isInvalid, setErrorMessage, setInvalidMethods inherited from interface com.vaadin.flow.data.binder.HasValidator
addValidationStatusChangeListenerMethods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, bindValue, clear, getEmptyValue, getOptionalValue, getValue, isEmpty, setValueMethods inherited from interface com.vaadin.flow.component.HasValueAndElement
bindReadOnly, bindRequiredIndicatorVisible, isReadOnly, setReadOnly
-
Constructor Details
-
Switch
public Switch()Default constructor. -
Switch
Constructs a switch with the initial label text.- Parameters:
labelText- the label text to set- See Also:
-
Switch
public Switch(boolean initialValue) Constructs a switch with the initial value.- Parameters:
initialValue- the initial value- See Also:
-
Switch
Constructs a switch with the initial label text and value.- Parameters:
labelText- the label text to setinitialValue- the initial value- See Also:
-
Switch
public Switch(String label, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch, Boolean>> listener) Constructs a switch with the initial label text and value change listener.- Parameters:
label- the label text to setlistener- the value change listener to add- See Also:
-
Switch
public Switch(boolean initialValue, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch, Boolean>> listener) Constructs a switch with the initial value and value change listener.- Parameters:
initialValue- the initial valuelistener- the value change listener to add- See Also:
-
Switch
public Switch(String labelText, boolean initialValue, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch, Boolean>> listener) Constructs a switch with the initial value, label text and value change listener.- Parameters:
labelText- the label text to setinitialValue- the initial valuelistener- the value change listener to add- See Also:
-
-
Method Details
-
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent) - Overrides:
onAttachin classcom.vaadin.flow.component.Component
-
setRequiredIndicatorVisible
public void setRequiredIndicatorVisible(boolean required) Sets whether the user is required to switch on the control. When required, an indicator appears next to the label and the field invalidates if the switch is first turned on and then turned off.NOTE: The required indicator is only visible when the field has a label, see
setLabel(String).- Specified by:
setRequiredIndicatorVisiblein interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch,Boolean>, Boolean> - Specified by:
setRequiredIndicatorVisiblein interfacecom.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch,Boolean>, Boolean> - Parameters:
required-trueto make the field required,falseotherwise- See Also:
-
isRequiredIndicatorVisible
public boolean isRequiredIndicatorVisible()Gets whether the user is required to switch on the control.- Specified by:
isRequiredIndicatorVisiblein interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch,Boolean>, Boolean> - Specified by:
isRequiredIndicatorVisiblein interfacecom.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<Switch,Boolean>, Boolean> - Returns:
trueif the field is required,falseotherwise- See Also:
-
getLabel
Get the current label text.- Specified by:
getLabelin interfacecom.vaadin.flow.component.HasLabel- Returns:
- the current label text
-
setLabel
Set the current label text of this switch.- Specified by:
setLabelin interfacecom.vaadin.flow.component.HasLabel- Parameters:
label- the label text to set
-
setLabelComponent
public void setLabelComponent(com.vaadin.flow.component.Component component) Replaces the label content with the given label component.- Parameters:
component- the component to be added to the label.
-
setAriaLabel
- Specified by:
setAriaLabelin interfacecom.vaadin.flow.component.HasAriaLabel
-
getAriaLabel
- Specified by:
getAriaLabelin interfacecom.vaadin.flow.component.HasAriaLabel
-
setAriaLabelledBy
- Specified by:
setAriaLabelledByin interfacecom.vaadin.flow.component.HasAriaLabel
-
getAriaLabelledBy
- Specified by:
getAriaLabelledByin interfacecom.vaadin.flow.component.HasAriaLabel
-
setAutofocus
public void setAutofocus(boolean autofocus) Set the switch to be input focused when the page loads.- Parameters:
autofocus- the boolean value to set
-
isAutofocus
public boolean isAutofocus()Get the state for the auto-focus property of the switch.- Returns:
- the
autofocusproperty from the switch
-
setManualValidation
public void setManualValidation(boolean enabled) - Specified by:
setManualValidationin interfacecom.vaadin.flow.component.HasValidation
-
getDefaultValidator
- Specified by:
getDefaultValidatorin interfacecom.vaadin.flow.data.binder.HasValidator<Boolean>
-
validate
protected void validate()Validates the current value against the constraints and sets theinvalidproperty and theerrorMessageproperty based on the result. If a custom error message is provided withHasValidationProperties.setErrorMessage(String), it is used. Otherwise, the error message defined in the i18n object is used.The method does nothing if the manual validation mode is enabled.
-
getI18n
Gets the internationalization object previously set for this component.NOTE: Updating the instance that is returned from this method will not update the component if not set again using
setI18n(SwitchI18n)- Returns:
- the i18n object or
nullif no i18n object has been set
-
setI18n
Sets the internationalization object for this component.- Parameters:
i18n- the i18n object, notnull
-