Class CheckboxGroup<T>
- All Implemented Interfaces:
AttachNotifier,DetachNotifier,HasAriaLabel,HasComponents,HasElement,HasEnabled,HasHelper,HasLabel,HasSize,HasStyle,HasTheme,HasValidation,HasValue<AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,,Set<T>>, Set<T>> HasValueAndElement<AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,,Set<T>>, Set<T>> HasClientValidation,HasThemeVariant<CheckboxGroupVariant>,HasTooltip,HasValidationProperties,InputField<AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,,Set<T>>, Set<T>> HasItemComponents<T>,HasValidator<Set<T>>,HasDataView<T,,Void, CheckboxGroupDataView<T>> HasListDataView<T,,CheckboxGroupListDataView<T>> 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.
- 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.shared.HasClientValidation
HasClientValidation.ClientValidatedEventNested classes/interfaces inherited from interface com.vaadin.flow.data.binder.HasItemComponents
HasItemComponents.ItemComponent<T>Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>> -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty checkbox groupCheckboxGroup(HasValue.ValueChangeListener<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, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>, Set<T>>> listener) Constructs a checkbox group with the defined label and a value change listener.CheckboxGroup(String label, HasValue.ValueChangeListener<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 TypeMethodDescriptionaddSelectionListener(MultiSelectionListener<CheckboxGroup<T>, T> listener) Adds a selection listener that will be called when the selection is changed either by the user or programmatically.Enables the implementing components to notify changes in their validation status to the observers.Gets the aria-label of the component.Gets the aria-labelledby of the componentDataProvider<T,?> Gets the data provider used by this CheckboxGroup.Gets the generic data view for the checkbox group.Returns the item enabled predicate.Gets the item label generator that is used to produce the strings shown in the checkbox group for each item.ComponentRenderer<? extends Component,T> Returns the item component renderer.getLabel()Gets the label of the checkbox group.Gets the list data view for the checkbox group.Returns an immutable set of the currently selected items.protected booleanChecks whether the element property has a value that can be converted to the model type.protected booleanIf true, the user cannot interact with this element.booleanReturns whether thisHasValueis in read-only mode or not.booleanDetermines whether the checkbox group is marked as input required.protected voidonAttach(AttachEvent attachEvent) Called when the component is attached to a UI.voidonEnabledStateChanged(boolean enabled) Handle component enable state when the enabled state changes.voidsetAriaLabel(String ariaLabel) Set the aria-label of the component to the given text.voidsetAriaLabelledBy(String labelledBy) Set the aria-labelledby of the component.voidsetDataProvider(DataProvider<T, ?> dataProvider) Sets a generic data provider for the CheckboxGroup to use.protected voidsetDisabled(boolean disabled) If true, the user cannot interact with this element.voidsetItemEnabledProvider(SerializablePredicate<T> itemEnabledProvider) Sets the item enabled predicate for this checkbox group.voidsetItemLabelGenerator(ItemLabelGenerator<T> itemLabelGenerator) Sets the item label generator that is used to produce the strings shown in the checkbox group for each item.setItems(DataProvider<T, Void> dataProvider) Set a generic data provider for the component to use and returns the baseDataViewthat provides API to get information on the items.setItems(InMemoryDataProvider<T> inMemoryDataProvider) Sets an in-memory data provider for the component to usesetItems(ListDataProvider<T> dataProvider) Sets a ListDataProvider for the component to use and returns aListDataViewthat provides information and allows operations on the items.voidSets the label for the checkbox group.voidsetManualValidation(boolean enabled) Sets whether manual validation mode is enabled for the component.voidsetReadOnly(boolean readOnly) Sets the read-only mode of thisHasValueto given mode.voidsetRenderer(ComponentRenderer<? extends Component, T> renderer) Sets the item renderer for this checkbox group.voidsetRequired(boolean required) Specifies that the user must fill in a value.voidSets the value of this component.voidupdateSelection(Set<T> addedItems, Set<T> removedItems) Updates the selection by adding and removing the given items from it.protected voidvalidate()protected booleanvalueEquals(Set<T> value1, Set<T> value2) Compares to value instances to each other to determine whether they are equal.Methods inherited from class com.vaadin.flow.component.AbstractSinglePropertyField
getSynchronizationRegistration, setPresentationValue, setSynchronizedEventMethods inherited from class com.vaadin.flow.component.AbstractField
addValueChangeListener, getEmptyValue, getValue, isEmpty, setModelValueMethods 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, 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.shared.HasClientValidation
addClientValidatedEventListenerMethods 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.HasHelper
getHelperComponent, getHelperText, setHelperComponent, setHelperTextMethods inherited from interface com.vaadin.flow.data.binder.HasItemComponents
addComponents, getItemPosition, prependComponentsMethods inherited from interface com.vaadin.flow.data.provider.HasListDataView
setItems, setItemsMethods 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.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
getDefaultValidatorMethods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, clear, getOptionalValue, isEmptyMethods inherited from interface com.vaadin.flow.component.HasValueAndElement
isRequiredIndicatorVisible, setRequiredIndicatorVisibleMethods 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:
-
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:
-
CheckboxGroup
public CheckboxGroup(HasValue.ValueChangeListener<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:
-
CheckboxGroup
public CheckboxGroup(String label, HasValue.ValueChangeListener<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:
-
CheckboxGroup
@SafeVarargs public CheckboxGroup(String label, HasValue.ValueChangeListener<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:
-
-
Method Details
-
onAttach
Description copied from class:ComponentCalled when the component is attached to a UI.The default implementation does nothing.
This method is invoked before the
AttachEventis fired for the component. -
setItems
Description copied from interface:HasDataViewSet a generic data provider for the component to use and returns the baseDataViewthat provides API to get information on the items.This method should be used only when the data provider type is not either
ListDataProviderorBackEndDataProvider.- Specified by:
setItemsin interfaceHasDataView<T,Void, CheckboxGroupDataView<T>> - Parameters:
dataProvider- DataProvider instance to use, notnull- Returns:
- DataView providing information on the data
-
setItems
Description copied from interface:HasDataViewSets an in-memory data provider for the component to useNote! Using a
ListDataProviderinstead of aInMemoryDataProvideris recommended to get access toListDataViewAPI by usingHasListDataView.setItems(ListDataProvider).- Specified by:
setItemsin interfaceHasDataView<T,Void, CheckboxGroupDataView<T>> - Parameters:
inMemoryDataProvider- InMemoryDataProvider to use, notnull- Returns:
- DataView providing information on the data
-
setItems
Description copied from interface:HasListDataViewSets a ListDataProvider for the component to use and returns aListDataViewthat provides information and allows operations on the items.- Specified by:
setItemsin interfaceHasListDataView<T,CheckboxGroupListDataView<T>> - Parameters:
dataProvider- ListDataProvider providing items to the component.- Returns:
- ListDataView providing access to the items
-
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: If the items are not in-memory an exception is thrown.- Specified by:
getListDataViewin interfaceHasListDataView<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:
getGenericDataViewin interfaceHasDataView<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
setItemsmethods 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
Description copied from interface:MultiSelectUpdates the selection by adding and removing the given items from it.If all the added items were already selected and the removed items were not selected, this is a NO-OP.
Duplicate items (in both add and remove sets) are ignored.
- Specified by:
updateSelectionin interfaceMultiSelect<CheckboxGroup<T>,T> - Parameters:
addedItems- the items to add, notnullremovedItems- the items to remove, notnull
-
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
nullvalues. 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:
setValuein interfaceHasValue<AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,Set<T>>, Set<T>> - Specified by:
setValuein interfaceMultiSelect<CheckboxGroup<T>,T> - Overrides:
setValuein classAbstractField<CheckboxGroup<T>,Set<T>> - Parameters:
value- the new value to set, notnull- Throws:
NullPointerException- if value isnull
-
getSelectedItems
Description copied from interface:MultiSelectReturns an immutable set of the currently selected items. It is safe to invoke otherSelectionModelmethods while iterating over the set.Implementation note: the iteration order of the items in the returned set should be well-defined and documented by the implementing class.
- Specified by:
getSelectedItemsin interfaceMultiSelect<CheckboxGroup<T>,T> - Returns:
- the items in the current selection, not
null
-
addSelectionListener
Description copied from interface:MultiSelectAdds a selection listener that will be called when the selection is changed either by the user or programmatically.- Specified by:
addSelectionListenerin interfaceMultiSelect<CheckboxGroup<T>,T> - Parameters:
listener- the value change listener, notnull- Returns:
- a registration for the listener
-
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) Description copied from class:ComponentHandle component enable state when the enabled state changes.By default this sets or removes the 'disabled' attribute from the element. This can be overridden to have custom handling.
- Overrides:
onEnabledStateChangedin classComponent- Parameters:
enabled- the new enabled state of the component
-
setReadOnly
public void setReadOnly(boolean readOnly) Description copied from interface:HasValueSets the read-only mode of thisHasValueto given mode. The user can't change the value when in read-only mode.A
HasValuewith a visual component in read-only mode typically looks visually different to signal to the user that the value cannot be edited.- Specified by:
setReadOnlyin interfaceHasValue<AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,Set<T>>, Set<T>> - Specified by:
setReadOnlyin interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,Set<T>>, Set<T>> - Parameters:
readOnly- a boolean value specifying whether the component is put read-only mode or not
-
isReadOnly
public boolean isReadOnly()Description copied from interface:HasValueReturns whether thisHasValueis in read-only mode or not.- Specified by:
isReadOnlyin interfaceHasValue<AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,Set<T>>, Set<T>> - Specified by:
isReadOnlyin interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<CheckboxGroup<T>,Set<T>>, Set<T>> - Returns:
falseif the user can modify the value,trueif not.
-
getItemEnabledProvider
Returns the item enabled predicate.- Returns:
- the item enabled predicate
- See Also:
-
setItemEnabledProvider
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
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
-
setLabel
Sets the label for the checkbox group. -
getLabel
Gets the label of the checkbox group. -
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:
labelledBy- 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
-
setRequired
public void setRequired(boolean required) Specifies that the user must fill in a value.- Parameters:
required- the boolean value to set
-
isRequired
public boolean isRequired()Determines whether the checkbox group is marked as input required.This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
- Returns:
trueif the input is required,falseotherwise
-
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
disabledproperty from the webcomponent
-
valueEquals
Description copied from class:AbstractFieldCompares to value instances to each other to determine whether they are equal. Equality is used to determine whether to update internal state and fire an event whenAbstractField.setValue(Object)orAbstractField.setModelValue(Object, boolean)is called. Subclasses can override this method to define an alternative comparison method instead ofObject.equals(Object).- Overrides:
valueEqualsin classAbstractField<CheckboxGroup<T>,Set<T>> - Parameters:
value1- the first instancevalue2- the second instance- Returns:
trueif the instances are equal; otherwisefalse
-
hasValidValue
protected boolean hasValidValue()Description copied from class:AbstractSinglePropertyFieldChecks whether the element property has a value that can be converted to the model type. Property changes from the element will be ignored if this method returnsfalse. The default implementation always returntrue.- Overrides:
hasValidValuein classAbstractSinglePropertyField<CheckboxGroup<T>,Set<T>> - Returns:
trueif the element property value can be converted to the model type; otherwisefalse
-
getItemRenderer
Returns the item component renderer.- Returns:
- the item renderer
- Since:
- 23.1
- See Also:
-
setRenderer
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
-
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() -
addValidationStatusChangeListener
public Registration addValidationStatusChangeListener(ValidationStatusChangeListener<Set<T>> listener) Description copied from interface:HasValidatorEnables the implementing components to notify changes in their validation status to the observers.Note: This method can be overridden by the implementing classes e.g. components, to enable the associated
Binder.Bindinginstance subscribing for their validation change events and revalidate itself.This method primarily designed for notifying the Binding about the validation status changes of a bound component at the client-side. WebComponents such as
<vaadin-date-picker>or any other component that accept a formatted text as input should be able to communicate their invalid status to their server-side instance, and a bound server-side component instance must notify its binding about this validation status change as well. When the binding instance revalidates, a chain of validators and convertors get executed one of which is the default validator provided byHasValidator.getDefaultValidator(). Thus, In order for the binding to be able to show/clear errors for its associated bound field, it is important that implementing components take that validation status into account while implementing any validator and converter includingHasValidator.getDefaultValidator(). Here is an example:@Tag("date-picker-demo") public class DatePickerDemo implements HasValidator<LocalDate> { // Each web component has a way to communicate its validation status // to its server-side component instance. The following clientSideValid // state is introduced here just for the sake of simplicity of this code // snippet: boolean clientSideValid = true; /** * Note howclientSideValidengaged in the definition * of this method. It is important to reflect this status either * in the returning validation result of this method or any other * validation that is associated with this component. */ @Override public Validator getDefaultValidator() { return (value, valueContext) -> clientSideValid ? ValidationResult.ok() : ValidationResult.error("Invalid date format"); } private final Collection<ValidationStatusChangeListener<LocalDate>> validationStatusListeners = new ArrayList<>(); /** * This enables the binding to subscribe for the validation status * change events that are fired by this component and revalidate * itself respectively. */ @Override public Registration addValidationStatusChangeListener( ValidationStatusChangeListener<LocalDate> listener) { validationStatusListeners.add(listener); return () -> validationStatusListeners.remove(listener); } private void fireValidationStatusChangeEvent( boolean newValidationStatus) { if (this.clientSideValid != newValidationStatus) { this.clientSideValid = newValidationStatus; var event = new ValidationStatusChangeEvent<>(this, newValidationStatus); validationStatusListeners.forEach( listener -> listener.validationStatusChanged(event)); } } }- Specified by:
addValidationStatusChangeListenerin interfaceHasValidator<T>- Returns:
- Registration of the added listener.
- See Also:
-