T
- the type of the items for the select@JavaScript(value="frontend://selectConnector.js") public class Select<T> extends GeneratedVaadinSelect<Select<T>,T> implements HasDataProvider<T>, HasItemsAndComponents<T>, HasSize, HasValidation, SingleSelect<Select<T>,T>, HasHelper, HasLabel, HasValidator<T>, HasClientValidation
This is a server side Java integration for <vaadin-select>
web
component.
For usage examples, see the demo in vaadin.com.
GeneratedVaadinSelect.InvalidChangeEvent<R extends GeneratedVaadinSelect<R,?>>, GeneratedVaadinSelect.OpenedChangeEvent<R extends GeneratedVaadinSelect<R,?>>
AbstractField.ComponentValueChangeEvent<C extends Component,V>
HasItemsAndComponents.ItemComponent<T>
HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>>
HasClientValidation.ClientValidatedEvent
BlurNotifier.BlurEvent<C extends Component>
FocusNotifier.FocusEvent<C extends Component>
Modifier and Type | Field and Description |
---|---|
static String |
LABEL_ATTRIBUTE |
Constructor and Description |
---|
Select()
Constructs a select.
|
Select(T... items)
Constructs a select with the given items.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Component... components)
Adds the given components as children of this component.
|
void |
addComponentAsFirst(Component component)
Adds the given component as the first child of this component.
|
void |
addComponentAtIndex(int index,
Component component)
Adds the given component as child of this component at the specific
index.
|
void |
addComponents(T afterItem,
Component... components)
Adds the components after the given item.
|
void |
addToPrefix(Component... components)
Adds the given components as children of this component at the slot
'prefix'.
|
Registration |
addValidationStatusChangeListener(ValidationStatusChangeListener<T> listener)
Enables the implementing components to notify changes in their validation
status to the observers.
|
Stream<Component> |
getChildren()
Gets the child components of this component.
|
DataProvider<T,?> |
getDataProvider()
Gets the data provider.
|
String |
getEmptySelectionCaption() |
String |
getErrorMessage()
Gets the error message to show to the user on invalid selection
|
SerializablePredicate<T> |
getItemEnabledProvider()
Returns the item enabled predicate.
|
ItemLabelGenerator<T> |
getItemLabelGenerator()
Gets the item label generator.
|
int |
getItemPosition(T item)
Gets the index of the child element that represents the given item.
|
ComponentRenderer<? extends Component,T> |
getItemRenderer()
Returns the item component renderer.
|
String |
getLabel()
Gets the string for the label element.
|
String |
getPlaceholder()
Gets the placeholder hint set for the user.
|
protected boolean |
hasValidValue()
Checks whether the element property has a value that can be converted to
the model type.
|
boolean |
isAutofocus()
Gets whether this select has been set to autofocus when the page loads.
|
boolean |
isEmptySelectionAllowed()
Returns whether the user is allowed to select nothing.
|
protected boolean |
isEnforcedFieldValidationEnabled() |
boolean |
isInvalid()
Gets whether the select is currently in invalid state.
|
boolean |
isRequiredIndicatorVisible()
Checks whether the required indicator is visible.
|
protected void |
onAttach(AttachEvent attachEvent)
Called when the component is attached to a UI.
|
void |
onEnabledStateChanged(boolean enabled)
Handle component enable state when the enabled state changes.
|
void |
prependComponents(T beforeItem,
Component... components)
Adds the components before the given item.
|
void |
remove(Component... components)
Removes the given child components from this component.
|
void |
removeAll()
Removes all child components that are not items.
|
void |
setAutofocus(boolean autofocus)
Sets the select to have focus when the page loads.
|
void |
setDataProvider(DataProvider<T,?> dataProvider)
Sets the data provider for this listing.
|
void |
setEmptySelectionAllowed(boolean emptySelectionAllowed)
Sets whether the user is allowed to select nothing.
|
void |
setEmptySelectionCaption(String emptySelectionCaption)
Sets the empty selection caption when
setEmptySelectionAllowed(boolean) has been enabled. |
void |
setErrorMessage(String errorMessage)
Sets the error message to show to the user on invalid selection.
|
void |
setInvalid(boolean invalid)
Sets the select to show as invalid state and display error message.
|
void |
setItemEnabledProvider(SerializablePredicate<T> itemEnabledProvider)
Sets the item enabled predicate for this select.
|
void |
setItemLabelGenerator(ItemLabelGenerator<T> itemLabelGenerator)
Sets the item label generator.
|
void |
setLabel(String label)
Sets the string for the label element.
|
void |
setPlaceholder(String placeholder)
Sets the placeholder hint for the user.
|
void |
setRenderer(ComponentRenderer<? extends Component,T> renderer)
Sets the item renderer for this select group.
|
void |
setRequiredIndicatorVisible(boolean requiredIndicatorVisible)
Sets the required indicator visible or not.
|
void |
setTextRenderer(ItemLabelGenerator<T> itemLabelGenerator)
Convenience setter for creating a
TextRenderer from the given
function that converts the item to a string. |
protected void |
validate()
Description copied from corresponding location in WebComponent:
|
addInvalidChangeListener, addOpenedChangeListener, getErrorMessageString, getLabelString, getNameString, getPlaceholderString, isAutofocusBoolean, isDisabledBoolean, isInvalidBoolean, isOpenedBoolean, isReadonlyBoolean, isRequiredBoolean, render, setDisabled, setName, setOpened, setReadonly, setRequired
getSynchronizationRegistration, setPresentationValue, setSynchronizedEvent
addValueChangeListener, getEmptyValue, getValue, isEmpty, setModelValue, setValue, valueEquals
addListener, fireEvent, from, get, getElement, getEventBus, getId, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, set, setElement, setId, setVisible
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
setItems
add
getCssSize, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull
isReadOnly, setReadOnly
addValueChangeListener, clear, getEmptyValue, getOptionalValue, getValue, isEmpty, setValue
isEnabled, setEnabled
getHelperComponent, getHelperText, setHelperComponent, setHelperText
getDefaultValidator
addClientValidatedEventListener
addClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
addFocusShortcut, blur, focus, getTabIndex, setTabIndex
addBlurListener
addFocusListener
addAttachListener
addDetachListener
public static final String LABEL_ATTRIBUTE
public Select()
public Select(T... items)
items
- the items for the selectpublic ComponentRenderer<? extends Component,T> getItemRenderer()
null
if none setsetRenderer(ComponentRenderer)
public void setRenderer(ComponentRenderer<? extends Component,T> renderer)
Default is null
which means that the item's Object.toString()
method is used and set as the text content of the vaadin item element.
renderer
- the item renderer, or null
to clearpublic void setTextRenderer(ItemLabelGenerator<T> itemLabelGenerator)
TextRenderer
from the given
function that converts the item to a string.
NOTE: even though this accepts an ItemLabelGenerator
,
this is not the same as
setItemLabelGenerator(ItemLabelGenerator)
which does a different
thing.
itemLabelGenerator
- the function that creates the text content from the item, not
null
public void setEmptySelectionAllowed(boolean emptySelectionAllowed)
true
a special empty item is shown to the user.
Default is false
. The empty selection item can be customized with
setEmptySelectionCaption(String)
.
emptySelectionAllowed
- true
to allow not selecting anything, false
to
require selectionsetEmptySelectionCaption(String)
public boolean isEmptySelectionAllowed()
true
if empty selection is allowed, false
otherwisepublic void setEmptySelectionCaption(String emptySelectionCaption)
setEmptySelectionAllowed(boolean)
has been enabled. The caption
is shown for the empty selection item in the drop down.
When the empty selection item is selected, the select shows the value
provided by setItemLabelGenerator(ItemLabelGenerator)
for the
null
item, or the string set with setPlaceholder(String)
or an empty string if not placeholder is set.
Default is an empty string "", which will show the place holder when selected.
emptySelectionCaption
- the empty selection caption to set, not null
setEmptySelectionAllowed(boolean)
public String getEmptySelectionCaption()
public SerializablePredicate<T> getItemEnabledProvider()
null
if not setsetItemEnabledProvider(com.vaadin.flow.function.SerializablePredicate<T>)
public void setItemEnabledProvider(SerializablePredicate<T> itemEnabledProvider)
true
) or disabled (false
). Disabled items are displayed
as grayed out and the user cannot select them.
By default is null
and all the items are enabled.
itemEnabledProvider
- the item enable predicate or null
to clearpublic ItemLabelGenerator<T> getItemLabelGenerator()
Default is null
.
null
if not setpublic void setItemLabelGenerator(ItemLabelGenerator<T> itemLabelGenerator)
Default is null
and the text content generated for the item with
setRenderer(ComponentRenderer)
is used instead.
itemLabelGenerator
- the item label generator to set, or null
to clearpublic String getPlaceholder()
null
if none setpublic void setPlaceholder(String placeholder)
The placeholder will be displayed in the case that there is no item selected, or the selected item has an empty string label, or the selected item has no label and it's DOM content is empty.
Default value is null
.
setPlaceholder
in class GeneratedVaadinSelect<Select<T>,T>
placeholder
- the placeholder to set, or null
to removepublic void setLabel(String label)
NOTE: the label must be set for the required indicator to be visible.
public String getLabel()
public void setAutofocus(boolean autofocus)
Default is false
.
setAutofocus
in class GeneratedVaadinSelect<Select<T>,T>
autofocus
- the autofocus to setpublic boolean isAutofocus()
true
if set to autofocus, false
if notpublic void setDataProvider(DataProvider<T,?> dataProvider)
HasDataProvider
setDataProvider
in interface HasDataProvider<T>
dataProvider
- the data provider, not nullpublic DataProvider<T,?> getDataProvider()
null
public void onEnabledStateChanged(boolean enabled)
Component
By default this sets or removes the 'disabled' attribute from the element. This can be overridden to have custom handling.
onEnabledStateChanged
in class Component
enabled
- the new enabled state of the componentpublic void setRequiredIndicatorVisible(boolean requiredIndicatorVisible)
If set visible, it is visually indicated in the user interface.
The method is intended to be used with Binder
which does
server-side validation. In case HTML element has its own (client-side)
validation it should be disabled when
setRequiredIndicatorVisible(true)
is called and re-enabled
back on setRequiredIndicatorVisible(false)
. It's
responsibility of each component implementation to follow the contract so
that the method call doesn't do anything else than show/hide the
"required" indication. Usually components provide their own
setRequired
method which should be called in case the
client-side validation is required.
NOTE: The required indicator will not be visible, if the
setLabel(String)
property is not set for the select.
setRequiredIndicatorVisible
in interface HasValue<AbstractField.ComponentValueChangeEvent<Select<T>,T>,T>
setRequiredIndicatorVisible
in interface HasValueAndElement<AbstractField.ComponentValueChangeEvent<Select<T>,T>,T>
requiredIndicatorVisible
- true
to make the required indicator visible,
false
if notpublic boolean isRequiredIndicatorVisible()
setLabel(String)
property is not set for the select.isRequiredIndicatorVisible
in interface HasValue<AbstractField.ComponentValueChangeEvent<Select<T>,T>,T>
isRequiredIndicatorVisible
in interface HasValueAndElement<AbstractField.ComponentValueChangeEvent<Select<T>,T>,T>
true
if visible, false
if notpublic void setErrorMessage(String errorMessage)
setErrorMessage
in interface HasValidation
setErrorMessage
in class GeneratedVaadinSelect<Select<T>,T>
errorMessage
- the error message or null
to clear itpublic String getErrorMessage()
getErrorMessage
in interface HasValidation
null
if not setpublic void setInvalid(boolean invalid)
setInvalid
in interface HasValidation
setInvalid
in class GeneratedVaadinSelect<Select<T>,T>
invalid
- true
for invalid, false
for validpublic boolean isInvalid()
isInvalid
in interface HasValidation
true
for invalid, false
for validpublic void add(Component... components)
In case the any of the specified components has already been added to another parent, it will be removed from there and added to this one.
NOTE: If you add a component with the slot
attribute
set, it will be placed in the light-dom of the vaadin-select
instead of the drop down, similar to addToPrefix(Component...)
add
in interface HasComponents
components
- the components to addpublic void addComponents(T afterItem, Component... components)
HasItemsAndComponents
addComponents
in interface HasItemsAndComponents<T>
afterItem
- item to add components aftercomponents
- components to add after itempublic void prependComponents(T beforeItem, Component... components)
HasItemsAndComponents
prependComponents
in interface HasItemsAndComponents<T>
beforeItem
- item to add components in front ofcomponents
- components to add before itempublic int getItemPosition(T item)
HasItemsAndComponents
getItemPosition
in interface HasItemsAndComponents<T>
item
- the item to look forpublic void addComponentAtIndex(int index, Component component)
In case the specified component has already been added to another parent, it will be removed from there and added to this one.
NOTE: If you add a component with the slot
attribute
set, it will be placed in the light-dom of the vaadin-select
instead of the drop down, similar to addToPrefix(Component...)
addComponentAtIndex
in interface HasComponents
index
- the index, where the component will be added. The index must
be non-negative and may not exceed the children countcomponent
- the component to add, value should not be nullpublic void addComponentAsFirst(Component component)
In case the specified component has already been added to another parent, it will be removed from there and added to this one.
NOTE: If you add a component with the slot
attribute
set, it will be placed in the light-dom of the vaadin-select
instead of the drop down, similar to addToPrefix(Component...)
addComponentAsFirst
in interface HasComponents
component
- the component to add, value should not be nullpublic void addToPrefix(Component... components)
GeneratedVaadinSelect
addToPrefix
in class GeneratedVaadinSelect<Select<T>,T>
components
- The components to add.public Stream<Component> getChildren()
Component
The default implementation finds child components by traversing each
child Element
tree.
If the component is injected to a PolymerTemplate using the
@Id
annotation the getChildren method will only return
children added from the server side and will not return any children
declared in the template file.
getChildren
in class Component
Id
public void remove(Component... components)
NOTE: any component with the slot
attribute will be
attempted to removed from the light dom of the vaadin-select, instead of
inside the options drop down.
remove
in interface HasComponents
remove
in class GeneratedVaadinSelect<Select<T>,T>
components
- the components to removeIllegalArgumentException
- if any of the components is not a child of this componentpublic void removeAll()
HasItems.setItems(Object[])
.
NOTE: this will remove all non-items from the drop down and any slotted components from vaadin-select's light dom.
removeAll
in interface HasComponents
removeAll
in class GeneratedVaadinSelect<Select<T>,T>
HasComponents.removeAll()
protected boolean hasValidValue()
AbstractSinglePropertyField
false
. The default implementation always
return true
.hasValidValue
in class AbstractSinglePropertyField<Select<T>,T>
true
if the element property value can be converted
to the model type; otherwise false
protected void onAttach(AttachEvent attachEvent)
Component
The default implementation does nothing.
This method is invoked before the AttachEvent
is fired for the
component.
protected void validate()
GeneratedVaadinSelect
Description copied from corresponding location in WebComponent:
Returns true if value
is valid, and sets the invalid
flag
appropriately.
This function is not supported by Flow because it returns a
boolean
. Functions with return types different than void are
not supported at this moment.
validate
in class GeneratedVaadinSelect<Select<T>,T>
public Registration addValidationStatusChangeListener(ValidationStatusChangeListener<T> listener)
HasValidator
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 by HasValidator.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 including HasValidator.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));
}
}
}
addValidationStatusChangeListener
in interface HasValidator<T>
Binder.BindingBuilderImpl.bind(ValueProvider,
Setter)
protected boolean isEnforcedFieldValidationEnabled()
Copyright © 2025. All rights reserved.