com.vaadin.flow.component.radiobutton.
Class RadioButtonGroup<T>
All Implemented Interfaces:
AttachNotifier
, DetachNotifier
, HasElement
, HasEnabled
, HasHelper
, HasLabel
, HasSize
, HasStyle
, HasTheme
, HasValidation
, HasValue<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>,
, HasValueAndElement<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>,
, HasClientValidation
, HasThemeVariant<RadioGroupVariant>
, HasTooltip
, HasValidationProperties
, HasValidator<T>
, HasDataView<T,
, HasListDataView<T,
, SingleSelect<RadioButtonGroup<T>,
, Serializable
Radio Button Group allows the user to select exactly one value from a list of related but mutually exclusive options.
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.ClientValidatedEvent
Nested classes/interfaces inherited from interface com.vaadin.flow.component.HasValue
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>>
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.
RadioButtonGroup
(HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>, T>> listener) Constructs a radio button group with a value change listener.
RadioButtonGroup
(String label) Creates an empty radio button group with the defined label.
RadioButtonGroup
(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>, T>> listener) Constructs a radio button group with the defined label and a value change listener.
RadioButtonGroup
(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>, T>> listener, T... items) Constructs a radio button group with the defined label, a value change listener and populated with the items in the array.
RadioButtonGroup
(String label, Collection<T> items) Creates a radio button group with the defined label and populated with the items in the collection.
RadioButtonGroup
(String label, T... items) Creates a radio button group with the defined label and populated with the items in the array.
-
Method Summary
Modifier and TypeMethodDescriptionEnables the implementing components to notify changes in their validation status to the observers.
Gets the generic data view for the RadioButtonGroup.
Returns the item enabled predicate.
Gets the item label generator that is used to produce the strings shown in the radio button group for each item.
ComponentRenderer<? extends Component,
T> Returns the item component renderer.
getLabel()
String used for the label element.
Gets the list data view for the RadioButtonGroup.
protected boolean
Checks whether the element property has a value that can be converted to the model type.
boolean
Returns whether this
HasValue
is in read-only mode or not.boolean
Specifies that the user must select a value
protected void
onAttach
(AttachEvent attachEvent) Called when the component is attached to a UI.
protected void
onDetach
(DetachEvent detachEvent) Called when the component is detached from a UI.
void
onEnabledStateChanged
(boolean enabled) Handle component enable state when the enabled state changes.
void
setItemEnabledProvider
(SerializablePredicate<T> itemEnabledProvider) Sets the item enabled predicate for this radio button group.
void
setItemLabelGenerator
(ItemLabelGenerator<T> itemLabelGenerator) Sets the item label generator that is used to produce the strings shown in the radio button group for each item.
setItems
(DataProvider<T, Void> dataProvider) Set a generic data provider for the component to use and returns the base
DataView
that provides API to get information on the items.setItems
(InMemoryDataProvider<T> inMemoryDataProvider) Sets an in-memory data provider for the component to use
setItems
(ListDataProvider<T> dataProvider) Sets a ListDataProvider for the component to use and returns a
ListDataView
that provides information and allows operations on the items.void
Sets the label for the field.
void
setReadOnly
(boolean readOnly) Sets the read-only mode of this
HasValue
to given mode.void
setRenderer
(ComponentRenderer<? extends Component, T> renderer) Sets the item renderer for this radio button group.
void
setRequired
(boolean required) Specifies that the user must select in a value.
void
Sets the value of this object.
protected void
validate()
protected boolean
valueEquals
(T value1, T value2) Compares two value instances to each other to determine whether they are equal.
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, 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.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.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
getDefaultValidator
Methods inherited from interface com.vaadin.flow.component.HasValue
addValueChangeListener, clear, getEmptyValue, getOptionalValue, getValue, isEmpty
Methods inherited from interface com.vaadin.flow.component.HasValueAndElement
isRequiredIndicatorVisible, setRequiredIndicatorVisible
-
Constructor Details
-
RadioButtonGroup
public RadioButtonGroup()Default constructor. Creates an empty radio button group.
-
RadioButtonGroup
Creates an empty radio button group with the defined label.
Parameters:
label
- the label describing the radio button groupSee Also:
-
RadioButtonGroup
Creates a radio button group with the defined label and populated with the items in the collection.
Parameters:
label
- the label describing the radio button groupitems
- the items to be shown in the list of the radio button groupSee Also:
-
RadioButtonGroup
Creates a radio button group with the defined label and populated with the items in the array.
Parameters:
label
- the label describing the radio button groupitems
- the items to be shown in the list of the radio button groupSee Also:
-
RadioButtonGroup
public RadioButtonGroup(HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>, T>> listener) Constructs a radio button group with a value change listener.
Parameters:
listener
- the value change listener to addSee Also:
-
RadioButtonGroup
public RadioButtonGroup(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>, T>> listener) Constructs a radio button group with the defined label and a value change listener.
Parameters:
label
- the label describing the radio button grouplistener
- the value change listener to addSee Also:
-
RadioButtonGroup
@SafeVarargs public RadioButtonGroup(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>, T>> listener, T... items) Constructs a radio button group with the defined label, a value change listener and populated with the items in the array.
Parameters:
label
- the label describing the radio button grouplistener
- the value change listener to additems
- the items to be shown in the list of the radio button groupSee Also:
-
-
Method Details
-
setItems
Description copied from interface:
HasDataView
Set a generic data provider for the component to use and returns the base
DataView
that provides API to get information on the items.This method should be used only when the data provider type is not either
ListDataProvider
orBackEndDataProvider
.Specified by:
setItems
in interfaceHasDataView<T,
Void, RadioButtonGroupDataView<T>> Parameters:
dataProvider
- DataProvider instance to use, notnull
Returns:
DataView providing information on the data
-
setItems
Description copied from interface:
HasDataView
Sets an in-memory data provider for the component to use
Note! Using a
ListDataProvider
instead of aInMemoryDataProvider
is recommended to get access toListDataView
API by usingHasListDataView.setItems(ListDataProvider)
.Specified by:
setItems
in interfaceHasDataView<T,
Void, RadioButtonGroupDataView<T>> Parameters:
inMemoryDataProvider
- InMemoryDataProvider to use, notnull
Returns:
DataView providing information on the data
-
setItems
Description copied from interface:
HasListDataView
Sets a ListDataProvider for the component to use and returns a
ListDataView
that provides information and allows operations on the items.Specified by:
setItems
in interfaceHasListDataView<T,
RadioButtonGroupListDataView<T>> Parameters:
dataProvider
- ListDataProvider providing items to the component.Returns:
ListDataView providing access to the items
-
getListDataView
Gets the list data view for the RadioButtonGroup. 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:
getListDataView
in interfaceHasListDataView<T,
RadioButtonGroupListDataView<T>> Returns:
the list data view that provides access to the data bound to the RadioButtonGroup
-
getGenericDataView
Gets the generic data view for the RadioButtonGroup. This data view should only be used when
getListDataView()
is not applicable for the underlying data provider.Specified by:
getGenericDataView
in interfaceHasDataView<T,
Void, RadioButtonGroupDataView<T>> Returns:
the generic DataView instance implementing
RadioButtonGroupDataView
-
hasValidValue
protected boolean hasValidValue()Description copied from class:
AbstractSinglePropertyField
Checks 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 returns
false
. The default implementation always returntrue
.Overrides:
hasValidValue
in classAbstractSinglePropertyField<RadioButtonGroup<T>,
T> Returns:
true
if the element property value can be converted to the model type; otherwisefalse
-
setItemLabelGenerator
Sets the item label generator that is used to produce the strings shown in the radio button 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 radio button group for each item.
Returns:
the item label generator used, not null
-
setValue
Description copied from interface:
HasValue
Sets the value of this object. If the new value is not equal to
getValue()
, fires a value change event. May throwIllegalArgumentException
if the value is not acceptable.Implementation note: the implementing class should document whether null values are accepted or not, and override
HasValue.getEmptyValue()
if the empty value is notnull
.Specified by:
setValue
in interfaceHasValue<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>,
T>, T> Overrides:
setValue
in classAbstractField<RadioButtonGroup<T>,
T> Parameters:
value
- the new value -
onAttach
Description copied from class:
Component
Called when the component is attached to a UI.
The default implementation does nothing.
This method is invoked before the
AttachEvent
is fired for the component. -
onDetach
Description copied from class:
Component
Called when the component is detached from a UI.
The default implementation does nothing.
This method is invoked before the
DetachEvent
is fired for the component. -
getItemEnabledProvider
Returns the item enabled predicate.
Returns:
the item enabled predicate
See Also:
-
setItemEnabledProvider
Sets the item enabled predicate for this radio button 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
-
getItemRenderer
Returns the item component renderer.
Returns:
the item renderer
See Also:
-
setRenderer
Sets the item renderer for this radio button group. The renderer is applied to each item to create a component which represents the item.
Note: Component acts as a label to the button and clicks on it trigger the radio button. Hence interactive components like DatePicker or ComboBox cannot be used.
Parameters:
renderer
- the item renderer, notnull
-
onEnabledStateChanged
public void onEnabledStateChanged(boolean enabled) Description copied from class:
Component
Handle 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:
onEnabledStateChanged
in classComponent
Parameters:
enabled
- the new enabled state of the component -
setReadOnly
public void setReadOnly(boolean readOnly) Description copied from interface:
HasValue
Sets the read-only mode of this
HasValue
to given mode. The user can't change the value when in read-only mode.A
HasValue
with a visual component in read-only mode typically looks visually different to signal to the user that the value cannot be edited.Specified by:
setReadOnly
in interfaceHasValue<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>,
T>, T> Specified by:
setReadOnly
in interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>,
T>, 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:
HasValue
Returns whether this
HasValue
is in read-only mode or not.Specified by:
isReadOnly
in interfaceHasValue<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>,
T>, T> Specified by:
isReadOnly
in interfaceHasValueAndElement<AbstractField.ComponentValueChangeEvent<RadioButtonGroup<T>,
T>, T> Returns:
false
if the user can modify the value,true
if not. -
setRequired
public void setRequired(boolean required) Specifies that the user must select in a value.
NOTE: The required indicator will not be visible, if there is no
label
property set for the RadioButtonGroup.Parameters:
required
- the boolean value to set -
isRequired
public boolean isRequired()Specifies that the user must select a value
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
required
property from the webcomponent -
setLabel
Sets the label for the field.
-
getLabel
String used for the label element.
-
valueEquals
Compares two 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 when
setValue(Object)
orAbstractField.setModelValue(Object, boolean)
is called. Subclasses can override this method to define an alternative comparison method instead ofObject.equals(Object)
.Overrides:
valueEquals
in classAbstractField<RadioButtonGroup<T>,
T> Parameters:
value1
- the first instancevalue2
- the second instanceReturns:
true
if the instances are equal; otherwisefalse
-
validate
protected void validate() -
addValidationStatusChangeListener
Description copied from interface:
HasValidator
Enables 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.Binding
instance 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 how
clientSideValid
engaged 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:
addValidationStatusChangeListener
in interfaceHasValidator<T>
Returns:
Registration of the added listener.
See Also:
-