Class CheckboxGroup<T>
- All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier
,com.vaadin.flow.component.DetachNotifier
,com.vaadin.flow.component.HasAriaLabel
,com.vaadin.flow.component.HasComponents
,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<CheckboxGroup<T>,
,Set<T>>, Set<T>> com.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,
,Set<T>>, Set<T>> HasClientValidation
,HasThemeVariant<CheckboxGroupVariant>
,HasTooltip
,HasValidationProperties
,InputField<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,
,Set<T>>, Set<T>> com.vaadin.flow.data.binder.HasItemComponents<T>
,com.vaadin.flow.data.binder.HasValidator<Set<T>>
,com.vaadin.flow.data.provider.HasDataView<T,
,Void, CheckboxGroupDataView<T>> com.vaadin.flow.data.provider.HasListDataView<T,
,CheckboxGroupListDataView<T>> com.vaadin.flow.data.selection.MultiSelect<CheckboxGroup<T>,
,T> Serializable
Use CheckBoxGroup to group related items. Individual checkboxes should be used for options that are not related to each other in any way.
Validation
CheckboxGroup comes with a built-in validation mechanism that verifies that
at least one checkbox is selected when
required
is enabled.
Validation is triggered whenever the user initiates a value change by toggling a checkbox. Programmatic value changes trigger validation as well. If validation fails, the component is marked as invalid and an error message is displayed below the group.
The required error message can be configured using either
CheckboxGroup.CheckboxGroupI18n.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.
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
The internationalization properties forCheckboxGroup
.static interface
CheckboxGroup.ItemHelperGenerator
can be used to generate helper text strings used by the checkbox group for each checkbox.Nested classes/interfaces inherited from class com.vaadin.flow.component.AbstractField
com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<C extends com.vaadin.flow.component.Component,
V extends Object> Nested classes/interfaces inherited from interface com.vaadin.flow.component.shared.HasClientValidation
HasClientValidation.ClientValidatedEvent
Nested classes/interfaces inherited from interface com.vaadin.flow.data.binder.HasItemComponents
com.vaadin.flow.data.binder.HasItemComponents.ItemComponent<T extends Object>
Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
com.vaadin.flow.component.HasValue.ValueChangeEvent<V extends Object>, com.vaadin.flow.component.HasValue.ValueChangeListener<E extends com.vaadin.flow.component.HasValue.ValueChangeEvent<?>>
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty checkbox groupCheckboxGroup
(com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>, Set<T>>> listener) Constructs a checkbox group with a value change listener.CheckboxGroup
(String label) Creates an empty checkbox group with the defined label.CheckboxGroup
(String label, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>, Set<T>>> listener) Constructs a checkbox group with the defined label and a value change listener.CheckboxGroup
(String label, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>, Set<T>>> listener, T... items) Constructs a checkbox group with the defined label, a value change listener and populated with the items in the array.CheckboxGroup
(String label, Collection<T> items) Creates a checkbox group with the defined label and populated with the items in the collection.CheckboxGroup
(String label, T... items) Creates a checkbox group with the defined label and populated with the items in the array. -
Method Summary
Modifier and TypeMethodDescriptioncom.vaadin.flow.shared.Registration
addSelectionListener
(com.vaadin.flow.data.selection.MultiSelectionListener<CheckboxGroup<T>, T> listener) com.vaadin.flow.data.provider.DataProvider<T,
?> Gets the data provider used by this CheckboxGroup.Gets the generic data view for the checkbox group.getI18n()
Gets the internationalization object previously set for this component.com.vaadin.flow.function.SerializablePredicate<T>
Returns the item enabled predicate.Gets theCheckboxGroup.ItemHelperGenerator
function that is used for generating helper text strings used by the checkbox group for each item.com.vaadin.flow.component.ItemLabelGenerator<T>
Gets the item label generator that is used to produce the strings shown in the checkbox group for each item.ComponentRenderer<? extends com.vaadin.flow.component.Component,
T> Returns the item component renderer.getLabel()
Gets the label of the checkbox group.Gets the list data view for the checkbox group.Gets the selection preservation mode.protected boolean
protected boolean
If true, the user cannot interact with this element.boolean
Alias forisRequiredIndicatorVisible()
boolean
Gets whether the user is required to select at least one checkbox.void
onEnabledStateChanged
(boolean enabled) void
setAriaLabel
(String ariaLabel) void
setAriaLabelledBy
(String labelledBy) void
setDataProvider
(com.vaadin.flow.data.provider.DataProvider<T, ?> dataProvider) Sets a generic data provider for the CheckboxGroup to use.protected void
setDisabled
(boolean disabled) If true, the user cannot interact with this element.void
Sets the internationalization object for this component.void
setItemEnabledProvider
(com.vaadin.flow.function.SerializablePredicate<T> itemEnabledProvider) Sets the item enabled predicate for this checkbox group.void
setItemHelperGenerator
(CheckboxGroup.ItemHelperGenerator<T> itemHelperGenerator) Sets theCheckboxGroup.ItemHelperGenerator
that is used for generating helper text strings used by the checkbox group for each item.void
setItemLabelGenerator
(com.vaadin.flow.component.ItemLabelGenerator<T> itemLabelGenerator) Sets the item label generator that is used to produce the strings shown in the checkbox group for each item.void
Sets the label for the checkbox group.void
setManualValidation
(boolean enabled) void
setRenderer
(ComponentRenderer<? extends com.vaadin.flow.component.Component, T> renderer) Sets the item renderer for this checkbox group.void
setRequired
(boolean required) Alias forsetRequiredIndicatorVisible(boolean)
.void
setRequiredIndicatorVisible
(boolean required) Sets whether the user is required to select at least one checkbox.void
setSelectionPreservationMode
(SelectionPreservationMode selectionPreservationMode) Sets the selection preservation mode.void
Sets the value of this component.void
updateSelection
(Set<T> addedItems, Set<T> removedItems) protected void
validate()
Validates the current value against the constraints and sets theinvalid
property and theerrorMessage
property based on the result.protected boolean
valueEquals
(Set<T> value1, Set<T> value2) Methods inherited from class com.vaadin.flow.component.AbstractSinglePropertyField
getSynchronizationRegistration, setPresentationValue, setSynchronizedEvent
Methods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, getEmptyValue, getValue, isEmpty, setModelValue
Methods 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, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListener
Methods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListener
Methods inherited from interface com.vaadin.flow.component.shared.HasClientValidation
addClientValidatedEventListener
Methods inherited from interface com.vaadin.flow.component.HasComponents
add, add, add, addComponentAsFirst, addComponentAtIndex, remove, remove, removeAll
Methods inherited from interface com.vaadin.flow.component.HasElement
getElement
Methods inherited from interface com.vaadin.flow.component.HasEnabled
isEnabled, setEnabled
Methods inherited from interface com.vaadin.flow.component.HasHelper
getHelperComponent, getHelperText, setHelperComponent, setHelperText
Methods inherited from interface com.vaadin.flow.data.binder.HasItemComponents
addComponents, getItemPosition, prependComponents
Methods inherited from interface com.vaadin.flow.data.provider.HasListDataView
setItems, setItems
Methods 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, setWidthFull
Methods inherited from interface com.vaadin.flow.component.HasStyle
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
Methods inherited from interface com.vaadin.flow.component.HasTheme
addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeName
Methods inherited from interface com.vaadin.flow.component.shared.HasThemeVariant
addThemeVariants, removeThemeVariants
Methods inherited from interface com.vaadin.flow.component.shared.HasTooltip
getTooltip, setTooltipText
Methods inherited from interface com.vaadin.flow.component.shared.HasValidationProperties
getErrorMessage, isInvalid, setErrorMessage, setInvalid
Methods inherited from interface com.vaadin.flow.data.binder.HasValidator
addValidationStatusChangeListener
Methods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, clear, getOptionalValue, isEmpty
Methods inherited from interface com.vaadin.flow.component.HasValueAndElement
isReadOnly, setReadOnly
Methods inherited from interface com.vaadin.flow.data.selection.MultiSelect
deselect, deselect, deselectAll, getEmptyValue, getValue, isSelected, select, select
-
Constructor Details
-
CheckboxGroup
public CheckboxGroup()Creates an empty checkbox group -
CheckboxGroup
Creates an empty checkbox group with the defined label.- Parameters:
label
- the label describing the checkbox group- See Also:
-
CheckboxGroup
Creates a checkbox group with the defined label and populated with the items in the collection.- Parameters:
label
- the label describing the checkbox groupitems
- the items to be shown in the list of the checkbox group- See Also:
-
setLabel(String)
HasListDataView.setItems(Collection)
-
CheckboxGroup
Creates a checkbox group with the defined label and populated with the items in the array.- Parameters:
label
- the label describing the checkbox groupitems
- the items to be shown in the list of the checkbox group- See Also:
-
setLabel(String)
HasListDataView.setItems(Object...)
-
CheckboxGroup
public CheckboxGroup(com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>, Set<T>>> listener) Constructs a checkbox group with a value change listener.- Parameters:
listener
- the value change listener to add- See Also:
-
AbstractField.addValueChangeListener(ValueChangeListener)
-
CheckboxGroup
public CheckboxGroup(String label, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>, Set<T>>> listener) Constructs a checkbox group with the defined label and a value change listener.- Parameters:
label
- the label describing the checkbox grouplistener
- the value change listener to add- See Also:
-
setLabel(String)
AbstractField.addValueChangeListener(ValueChangeListener)
-
CheckboxGroup
@SafeVarargs public CheckboxGroup(String label, com.vaadin.flow.component.HasValue.ValueChangeListener<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>, Set<T>>> listener, T... items) Constructs a checkbox group with the defined label, a value change listener and populated with the items in the array.- Parameters:
label
- the label describing the checkbox grouplistener
- the value change listener to additems
- the items to be shown in the list of the checkbox group- See Also:
-
setLabel(String)
AbstractField.addValueChangeListener(ValueChangeListener)
HasListDataView.setItems(Object...)
-
-
Method Details
-
setItems
public CheckboxGroupDataView<T> setItems(com.vaadin.flow.data.provider.DataProvider<T, Void> dataProvider) - Specified by:
setItems
in interfacecom.vaadin.flow.data.provider.HasDataView<T,
Void, CheckboxGroupDataView<T>>
-
setItems
public CheckboxGroupDataView<T> setItems(com.vaadin.flow.data.provider.InMemoryDataProvider<T> inMemoryDataProvider) - Specified by:
setItems
in interfacecom.vaadin.flow.data.provider.HasDataView<T,
Void, CheckboxGroupDataView<T>>
-
setItems
public CheckboxGroupListDataView<T> setItems(com.vaadin.flow.data.provider.ListDataProvider<T> dataProvider) - Specified by:
setItems
in interfacecom.vaadin.flow.data.provider.HasListDataView<T,
CheckboxGroupListDataView<T>>
-
getListDataView
Gets the list data view for the checkbox group. This data view should only be used when the items are in-memory and set with:HasListDataView.setItems(Collection)
HasListDataView.setItems(Object[])
setItems(ListDataProvider)
- Specified by:
getListDataView
in interfacecom.vaadin.flow.data.provider.HasListDataView<T,
CheckboxGroupListDataView<T>> - Returns:
- the list data view that provides access to the data bound to the checkbox group
-
getGenericDataView
Gets the generic data view for the checkbox group. This data view should only be used whengetListDataView()
is not applicable for the underlying data provider.- Specified by:
getGenericDataView
in interfacecom.vaadin.flow.data.provider.HasDataView<T,
Void, CheckboxGroupDataView<T>> - Returns:
- the generic DataView instance implementing
CheckboxGroupDataView
-
setDataProvider
Sets a generic data provider for the CheckboxGroup to use.Use this method when none of the
setItems
methods are applicable, e.g. when having a data provider with filter that cannot be transformed toDataProvider<T, Void>
.- Parameters:
dataProvider
- DataProvider instance to use, notnull
-
updateSelection
- Specified by:
updateSelection
in interfacecom.vaadin.flow.data.selection.MultiSelect<CheckboxGroup<T>,
T>
-
setValue
Sets the value of this component. If the new value is not equal to the previous value, fires a value change event.The component doesn't accept
null
values. The value of a checkbox group without any selected items is an empty set. You can use theHasValue.clear()
method to set the empty value.- Specified by:
setValue
in interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,
Set<T>>, Set<T>> - Specified by:
setValue
in interfacecom.vaadin.flow.data.selection.MultiSelect<CheckboxGroup<T>,
T> - Overrides:
setValue
in classcom.vaadin.flow.component.AbstractField<CheckboxGroup<T>,
Set<T>> - Parameters:
value
- the new value to set, notnull
- Throws:
NullPointerException
- if value isnull
-
getSelectedItems
- Specified by:
getSelectedItems
in interfacecom.vaadin.flow.data.selection.MultiSelect<CheckboxGroup<T>,
T>
-
addSelectionListener
public com.vaadin.flow.shared.Registration addSelectionListener(com.vaadin.flow.data.selection.MultiSelectionListener<CheckboxGroup<T>, T> listener) - Specified by:
addSelectionListener
in interfacecom.vaadin.flow.data.selection.MultiSelect<CheckboxGroup<T>,
T>
-
getDataProvider
Gets the data provider used by this CheckboxGroup.To get information and control over the items in the CheckboxGroup, use either
getListDataView()
orgetGenericDataView()
instead.- Returns:
- the data provider used by this CheckboxGroup
-
onEnabledStateChanged
public void onEnabledStateChanged(boolean enabled) - Overrides:
onEnabledStateChanged
in classcom.vaadin.flow.component.Component
-
getItemEnabledProvider
Returns the item enabled predicate.- Returns:
- the item enabled predicate
- See Also:
-
setItemEnabledProvider
public void setItemEnabledProvider(com.vaadin.flow.function.SerializablePredicate<T> itemEnabledProvider) Sets the item enabled predicate for this checkbox group. The predicate is applied to each item to determine whether the item should be enabled (true
) or disabled (false
). Disabled items are displayed as grayed out and the user cannot select them. The default predicate always returns true (all the items are enabled).- Parameters:
itemEnabledProvider
- the item enable predicate, notnull
-
setItemLabelGenerator
public void setItemLabelGenerator(com.vaadin.flow.component.ItemLabelGenerator<T> itemLabelGenerator) Sets the item label generator that is used to produce the strings shown in the checkbox group for each item. By default,String.valueOf(Object)
is used.Setting an item label generator removes any previously set item renderer.
- Parameters:
itemLabelGenerator
- the item label provider to use, not null
-
getItemLabelGenerator
Gets the item label generator that is used to produce the strings shown in the checkbox group for each item.- Returns:
- the item label generator used, not null
-
setItemHelperGenerator
Sets theCheckboxGroup.ItemHelperGenerator
that is used for generating helper text strings used by the checkbox group for each item.- Parameters:
itemHelperGenerator
- the item helper generator to use, not null- Since:
- 24.4
- See Also:
-
HasHelper.setHelperText(String)
setItemLabelGenerator(com.vaadin.flow.component.ItemLabelGenerator<T>)
-
getItemHelperGenerator
Gets theCheckboxGroup.ItemHelperGenerator
function that is used for generating helper text strings used by the checkbox group for each item.- Returns:
- the item helper generator, not null
- Since:
- 24.4
- See Also:
-
setLabel
Sets the label for the checkbox group.- Specified by:
setLabel
in interfacecom.vaadin.flow.component.HasLabel
- Parameters:
label
- value for thelabel
property in the checkbox group
-
getLabel
Gets the label of the checkbox group.- Specified by:
getLabel
in interfacecom.vaadin.flow.component.HasLabel
- Returns:
- the
label
property of the checkbox group
-
setAriaLabel
- Specified by:
setAriaLabel
in interfacecom.vaadin.flow.component.HasAriaLabel
-
getAriaLabel
- Specified by:
getAriaLabel
in interfacecom.vaadin.flow.component.HasAriaLabel
-
setAriaLabelledBy
- Specified by:
setAriaLabelledBy
in interfacecom.vaadin.flow.component.HasAriaLabel
-
getAriaLabelledBy
- Specified by:
getAriaLabelledBy
in interfacecom.vaadin.flow.component.HasAriaLabel
-
setRequiredIndicatorVisible
public void setRequiredIndicatorVisible(boolean required) Sets whether the user is required to select at least one checkbox. When required, an indicator appears next to the label and the field invalidates if all previously selected checkboxes are deselected.NOTE: The required indicator is only visible when the field has a label, see
setLabel(String)
.- Specified by:
setRequiredIndicatorVisible
in interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,
Set<T>>, Set<T>> - Specified by:
setRequiredIndicatorVisible
in interfacecom.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,
Set<T>>, Set<T>> - Parameters:
required
-true
to make the field required,false
otherwise- See Also:
-
isRequiredIndicatorVisible
public boolean isRequiredIndicatorVisible()Gets whether the user is required to select at least one checkbox.- Specified by:
isRequiredIndicatorVisible
in interfacecom.vaadin.flow.component.HasValue<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,
Set<T>>, Set<T>> - Specified by:
isRequiredIndicatorVisible
in interfacecom.vaadin.flow.component.HasValueAndElement<com.vaadin.flow.component.AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,
Set<T>>, Set<T>> - Returns:
true
if the field is required,false
otherwise- See Also:
-
isRequired
public boolean isRequired()Alias forisRequiredIndicatorVisible()
- Returns:
true
if the field is required,false
otherwise
-
setRequired
public void setRequired(boolean required) Alias forsetRequiredIndicatorVisible(boolean)
.- Parameters:
required
-true
to make the field required,false
otherwise
-
setDisabled
protected void setDisabled(boolean disabled) If true, the user cannot interact with this element.- Parameters:
disabled
- the boolean value to set
-
isDisabledBoolean
protected boolean isDisabledBoolean()If true, the user cannot interact with this element.- Returns:
- the
disabled
property from the webcomponent
-
valueEquals
- Overrides:
valueEquals
in classcom.vaadin.flow.component.AbstractField<CheckboxGroup<T>,
Set<T>>
-
hasValidValue
protected boolean hasValidValue()- Overrides:
hasValidValue
in classcom.vaadin.flow.component.AbstractSinglePropertyField<CheckboxGroup<T>,
Set<T>>
-
getItemRenderer
Returns the item component renderer.- Returns:
- the item renderer
- Since:
- 23.1
- See Also:
-
setRenderer
public void setRenderer(ComponentRenderer<? extends com.vaadin.flow.component.Component, T> renderer) Sets the item renderer for this checkbox group. The renderer is applied to each item to create a component which represents the item.Note: Component acts as a label to the checkbox and clicks on it trigger the checkbox. Hence interactive components like DatePicker or ComboBox cannot be used.
- Parameters:
renderer
- the item renderer, notnull
- Since:
- 23.1
-
getSelectionPreservationMode
Gets the selection preservation mode.- Returns:
- the selection preservation mode
- See Also:
-
setManualValidation
public void setManualValidation(boolean enabled) - Specified by:
setManualValidation
in interfacecom.vaadin.flow.component.HasValidation
-
getDefaultValidator
- Specified by:
getDefaultValidator
in interfacecom.vaadin.flow.data.binder.HasValidator<T>
-
validate
protected void validate()Validates the current value against the constraints and sets theinvalid
property and theerrorMessage
property 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(CheckboxGroupI18n)
- Returns:
- the i18n object or
null
if no i18n object has been set
-
setI18n
Sets the internationalization object for this component.- Parameters:
i18n
- the i18n object, notnull
-