Class Checkbox
- All Implemented Interfaces:
AttachNotifier,BlurNotifier<Checkbox>,ClickNotifier<Checkbox>,DetachNotifier,Focusable<Checkbox>,FocusNotifier<Checkbox>,HasAriaLabel,HasElement,HasEnabled,HasHelper,HasLabel,HasSize,HasStyle,HasValidation,HasValue<AbstractField.ComponentValueChangeEvent<Checkbox,,Boolean>, Boolean> HasValueAndElement<AbstractField.ComponentValueChangeEvent<Checkbox,,Boolean>, Boolean> HasClientValidation,HasTooltip,HasValidationProperties,InputField<AbstractField.ComponentValueChangeEvent<Checkbox,,Boolean>, Boolean> HasValidator<Boolean>,Serializable
Checkbox also has an indeterminate mode, see isIndeterminate() for
more info.
Use CheckboxGroup to group related
items. Individual checkboxes should be used for options that are not related
to each other in any way.
- Author:
- Vaadin Ltd
- 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.shared.HasClientValidation
HasClientValidation.ClientValidatedEventNested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>> -
Constructor Summary
ConstructorsConstructorDescriptionCheckbox()Default constructor.Checkbox(boolean initialValue) Constructs a checkbox with the initial value.Checkbox(boolean initialValue, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<Checkbox, Boolean>> listener) Constructs a checkbox with the initial value and value change listener.Constructs a checkbox with the initial label text.Constructs a checkbox with the initial value.Checkbox(String labelText, boolean initialValue, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<Checkbox, Boolean>> listener) Constructs a checkbox with the initial value, label text and value change listener.Checkbox(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<Checkbox, Boolean>> listener) Constructs a checkbox with the initial label text and value change listener. -
Method Summary
Modifier and TypeMethodDescriptionGets the aria-label of the component.Gets the aria-labelledby of the componentgetLabel()Get the current label text.booleanGet the state for the auto-focus property of the checkbox.booleanGet the indeterminate state of the checkbox.protected voidonAttach(AttachEvent attachEvent) Called when the component is attached to a UI.voidsetAriaLabel(String ariaLabel) Set the aria-label of the component to the given text.voidsetAriaLabelledBy(String ariaLabelledBy) Set the aria-labelledby of the component.voidsetAutofocus(boolean autofocus) Set the checkbox to be input focused when the page loads.voidsetIndeterminate(boolean indeterminate) Set the indeterminate state of the checkbox.voidSet the current label text of this checkbox.voidsetLabelComponent(Component component) Replaces the label content with the given label component.voidsetManualValidation(boolean enabled) Sets whether manual validation mode is enabled for the component.protected voidvalidate()Performs server-side validation of the current value.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, 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.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, getTabIndex, setTabIndexMethods inherited from interface com.vaadin.flow.component.FocusNotifier
addFocusListenerMethods inherited from interface com.vaadin.flow.component.shared.HasClientValidation
addClientValidatedEventListenerMethods 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.HasHelper
getHelperComponent, getHelperText, setHelperComponent, setHelperTextMethods 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.shared.HasTooltip
getTooltip, setTooltipTextMethods inherited from interface com.vaadin.flow.component.shared.HasValidationProperties
getErrorMessage, isInvalid, setErrorMessage, setInvalidMethods inherited from interface com.vaadin.flow.data.binder.HasValidator
addValidationStatusChangeListener, getDefaultValidatorMethods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, clear, getEmptyValue, getOptionalValue, getValue, isEmpty, setValueMethods inherited from interface com.vaadin.flow.component.HasValueAndElement
isReadOnly, isRequiredIndicatorVisible, setReadOnly, setRequiredIndicatorVisible
-
Constructor Details
-
Checkbox
public Checkbox()Default constructor. -
Checkbox
Constructs a checkbox with the initial label text.- Parameters:
labelText- the label text to set- See Also:
-
Checkbox
public Checkbox(boolean initialValue) Constructs a checkbox with the initial value.- Parameters:
initialValue- the initial value- See Also:
-
Checkbox
Constructs a checkbox with the initial value.- Parameters:
labelText- the label text to setinitialValue- the initial value- See Also:
-
Checkbox
public Checkbox(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<Checkbox, Boolean>> listener) Constructs a checkbox with the initial label text and value change listener.- Parameters:
label- the label text to setlistener- the value change listener to add- See Also:
-
Checkbox
public Checkbox(boolean initialValue, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<Checkbox, Boolean>> listener) Constructs a checkbox with the initial value and value change listener.- Parameters:
initialValue- the initial valuelistener- the value change listener to add- See Also:
-
Checkbox
public Checkbox(String labelText, boolean initialValue, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<Checkbox, Boolean>> listener) Constructs a checkbox 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
Description copied from class:ComponentCalled when the component is attached to a UI.This method is invoked before the
Make sure to callAttachEventis fired for the component.super.onAttachwhen overriding this method. -
getLabel
Get the current label text. -
setLabel
Set the current label text of this checkbox. -
setLabelComponent
Replaces the label content with the given label component.- Parameters:
component- the component to be added to the label.- Since:
- 23.1
-
setAriaLabel
Description copied from interface:HasAriaLabelSet the aria-label of the component to the given text.This method should not be used if
HasAriaLabel.setAriaLabelledBy(String)is also used. If both attributes are present, aria-labelledby will take precedence over aria-label.- Specified by:
setAriaLabelin interfaceHasAriaLabel- Parameters:
ariaLabel- the aria-label text to set ornullto clear
-
getAriaLabel
Description copied from interface:HasAriaLabelGets the aria-label of the component.- Specified by:
getAriaLabelin interfaceHasAriaLabel- Returns:
- an optional aria-label of the component if no aria-label has been set
-
setAriaLabelledBy
Description copied from interface:HasAriaLabelSet the aria-labelledby of the component. The value must be a valid id attribute of another element that labels the component. The label element must be in the same DOM scope of the component, otherwise screen readers may fail to announce the label content properly.This method should not be used if
HasAriaLabel.setAriaLabel(String)is also used. If both attributes are present, aria-labelledby will take precedence over aria-label.- Specified by:
setAriaLabelledByin interfaceHasAriaLabel- Parameters:
ariaLabelledBy- the string with the id of the element that will be used as label ornullto clear
-
getAriaLabelledBy
Description copied from interface:HasAriaLabelGets the aria-labelledby of the component- Specified by:
getAriaLabelledByin interfaceHasAriaLabel- Returns:
- an optional aria-labelledby of the component if no aria-labelledby has been set
-
setAutofocus
public void setAutofocus(boolean autofocus) Set the checkbox 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 checkbox.This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
- Returns:
- the
autofocusproperty from the checkbox
-
setIndeterminate
public void setIndeterminate(boolean indeterminate) Set the indeterminate state of the checkbox.NOTE: As according to the HTML5 standard, this has only effect on the visual appearance, not on the checked value!
- Parameters:
indeterminate- the boolean value to set- See Also:
-
isIndeterminate
@Synchronize(property="indeterminate", value="indeterminate-changed") public boolean isIndeterminate()Get the indeterminate state of the checkbox. The default value isfalse.An indeterminate checkbox is neither checked nor unchecked. A typical use case is a “Select All” checkbox indicating that some, but not all, items are selected. When the user clicks an indeterminate checkbox, it is no longer indeterminate, and the
checkedvalue also changes.NOTE: As according to the HTML5 standard, this has only effect on the visual appearance, not on the checked value!
- Returns:
- the
indeterminateproperty from the checkbox
-
setManualValidation
public void setManualValidation(boolean enabled) Description copied from interface:HasValidationSets whether manual validation mode is enabled for the component.When enabled, the component doesn't perform its built-in constraint validation on value change, blur, and other events. This allows manually controlling the invalid state and error messages using the
HasValidation.setInvalid(boolean)andHasValidation.setErrorMessage(String)methods. Manual mode is helpful when there is a need for a totally custom validation logic that cannot be achieved with Binder.Example:
Field field = new Field(); field.setManualValidation(true); field.addValueChangeListener(event -> { if (Objects.equal(event.getValue(), "")) { field.setInvalid(true); field.setErrorMessage("The field is required."); } else { field.setInvalid(false); } });For components that don't have built-in validation, the method has no effect.
- Specified by:
setManualValidationin interfaceHasValidation- Parameters:
enabled- whether to enable manual validation mode.
-
validate
protected void validate()Performs server-side validation of the current value. This is needed because it is possible to circumvent the client-side validation constraints using browser development tools.
-