@JsModule(value="./datepickerConnector.js") @JavaScript(value="frontend://datepickerConnector.js") @NpmPackage(value="date-fns", version="2.29.3") public class DatePicker extends GeneratedVaadinDatePicker<DatePicker,LocalDate> implements HasSize, HasValidation, HasHelper, HasLabel, HasValidator<LocalDate>, HasClientValidation
vaadin-date-picker webcomponent.
It allows setting and getting LocalDate objects, setting minimum and
maximum date ranges and has internationalization support by using the
DatePicker.DatePickerI18n object.
| Modifier and Type | Class and Description |
|---|---|
static class |
DatePicker.DatePickerI18n
The internationalization properties for
DatePicker. |
GeneratedVaadinDatePicker.ChangeEvent<R extends GeneratedVaadinDatePicker<R,?>>, GeneratedVaadinDatePicker.InvalidChangeEvent<R extends GeneratedVaadinDatePicker<R,?>>, GeneratedVaadinDatePicker.OpenedChangeEvent<R extends GeneratedVaadinDatePicker<R,?>>AbstractField.ComponentValueChangeEvent<C extends Component,V>HasClientValidation.ClientValidatedEventBlurNotifier.BlurEvent<C extends Component>FocusNotifier.FocusEvent<C extends Component>HasValue.ValueChangeEvent<V>, HasValue.ValueChangeListener<E extends HasValue.ValueChangeEvent<?>>| Constructor and Description |
|---|
DatePicker()
Default constructor.
|
DatePicker(HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>> listener)
Convenience constructor to create a date picker with a
ValueChangeListener. |
DatePicker(LocalDate initialDate)
Convenience constructor to create a date picker with a pre-selected date
in current UI locale format.
|
DatePicker(LocalDate initialDate,
HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>> listener)
Convenience constructor to create a date picker with a pre-selected date
in current UI locale format and a
ValueChangeListener. |
DatePicker(LocalDate initialDate,
Locale locale)
Convenience Constructor to create a date picker with pre-selected date
and locale setup.
|
DatePicker(String label)
Convenience constructor to create a date picker with a label.
|
DatePicker(String label,
HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>> listener)
Convenience constructor to create a date picker with a
ValueChangeListener and a label. |
DatePicker(String label,
LocalDate initialDate)
Convenience constructor to create a date picker with a pre-selected date
in current UI locale format and a label.
|
DatePicker(String label,
LocalDate initialDate,
HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>> listener)
Convenience constructor to create a date picker with a pre-selected date
in current UI locale format, a
ValueChangeListener and a label. |
| Modifier and Type | Method and Description |
|---|---|
Registration |
addInvalidChangeListener(ComponentEventListener<GeneratedVaadinDatePicker.InvalidChangeEvent<DatePicker>> listener)
Adds a listener for
invalid-changed events fired by the
webcomponent. |
Registration |
addOpenedChangeListener(ComponentEventListener<GeneratedVaadinDatePicker.OpenedChangeEvent<DatePicker>> listener)
Adds a listener for
opened-changed events fired by the
webcomponent. |
Registration |
addValidationStatusChangeListener(ValidationStatusChangeListener<LocalDate> listener)
Enables the implementing components to notify changes in their validation
status to the observers.
|
protected void |
close()
Closes the datepicker overlay.
|
Validator<LocalDate> |
getDefaultValidator()
Returns a validator that checks the state of the Value.
|
String |
getErrorMessage()
Gets the current error message from the datepicker.
|
DatePicker.DatePickerI18n |
getI18n()
Gets the internationalization object previously set for this component.
|
LocalDate |
getInitialPosition()
Get the visible date when there is no value selected.
|
String |
getLabel()
Gets the label of the datepicker.
|
Locale |
getLocale()
Gets the Locale for this date picker
|
LocalDate |
getMax()
Gets the maximum date in the date picker.
|
LocalDate |
getMin()
Gets the minimum date in the date picker.
|
String |
getName()
Gets the name of the DatePicker.
|
String |
getPlaceholder()
Gets the placeholder of the datepicker.
|
boolean |
isAutoOpen()
When auto open is enabled, the dropdown will open when the field is
clicked.
|
boolean |
isClearButtonVisible()
Gets whether this datepicker displays a clear button when it has value.
|
protected boolean |
isEnforcedFieldValidationEnabled()
Whether the full experience validation is enforced for the component.
|
boolean |
isInvalid()
Gets the validity of the datepicker output.
|
boolean |
isOpened()
Gets the states of the drop-down for the datepicker
|
boolean |
isRequired()
Determines whether the datepicker is marked as input required.
|
boolean |
isWeekNumbersVisible()
Get the state of
showWeekNumbers property of the datepicker |
protected void |
onAttach(AttachEvent attachEvent)
Called when the component is attached to a UI.
|
void |
open()
Opens the datepicker overlay.
|
void |
setAutoOpen(boolean autoOpen)
When auto open is enabled, the dropdown will open when the field is
clicked.
|
void |
setClearButtonVisible(boolean clearButtonVisible)
Sets displaying a clear button in the datepicker when it has value.
|
void |
setErrorMessage(String errorMessage)
Description copied from corresponding location in WebComponent:
|
void |
setI18n(DatePicker.DatePickerI18n i18n)
Sets the internationalization properties for this component.
|
void |
setInitialPosition(LocalDate initialPosition)
Date which should be visible when there is no value selected.
|
void |
setInvalid(boolean invalid)
Description copied from corresponding location in WebComponent:
|
void |
setLabel(String label)
Sets the label for the datepicker.
|
void |
setLocale(Locale locale)
Set the Locale for the Date Picker.
|
void |
setMax(LocalDate max)
Sets the maximum date in the date picker.
|
void |
setMin(LocalDate min)
Sets the minimum date in the date picker.
|
void |
setName(String name)
Description copied from corresponding location in WebComponent:
|
void |
setOpened(boolean opened)
Sets the opened property of the datepicker to open or close its overlay.
|
void |
setPlaceholder(String placeholder)
Description copied from corresponding location in WebComponent:
|
void |
setRequired(boolean required)
Description copied from corresponding location in WebComponent:
|
void |
setRequiredIndicatorVisible(boolean required)
Sets the required indicator visible or not.
|
void |
setValue(LocalDate value)
Sets the value of this object.
|
void |
setWeekNumbersVisible(boolean weekNumbersVisible)
Set the week number visible in the DatePicker.
|
protected void |
validate()
Performs server-side validation of the current value.
|
addChangeListener, addToPrefix, checkValidity, getErrorMessageString, getI18nJsonObject, getInitialPositionString, getLabelString, getMaxAsStringString, getMinAsStringString, getNameString, getPlaceholderString, isAutofocusBoolean, isClearButtonVisibleBoolean, isDisabledBoolean, isInvalidBoolean, isOpenedBoolean, isReadonlyBoolean, isRequiredBoolean, isShowWeekNumbersBoolean, remove, removeAll, setAutofocus, setDisabled, setI18n, setInitialPosition, setMaxAsString, setMinAsString, setReadonly, setShowWeekNumbers, validategetSynchronizationRegistration, hasValidValue, setPresentationValue, setSynchronizedEventaddValueChangeListener, getEmptyValue, getValue, isEmpty, setModelValue, valueEqualsaddListener, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, set, setElement, setId, setVisibleclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitgetCssSize, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFullgetHelperComponent, getHelperText, setHelperComponent, setHelperTextgetElementaddClientValidatedEventListeneraddClassName, addClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassNameaddFocusShortcut, blur, focus, getTabIndex, setTabIndexaddBlurListeneraddFocusListenerisEnabled, setEnabledisReadOnly, isRequiredIndicatorVisible, setReadOnlyclear, getOptionalValueaddAttachListeneraddDetachListenerpublic DatePicker()
public DatePicker(LocalDate initialDate)
initialDate - the pre-selected date in the pickerAbstractField.setValue(Object)public DatePicker(String label)
label - the label describing the date pickersetLabel(String)public DatePicker(String label, LocalDate initialDate)
label - the label describing the date pickerinitialDate - the pre-selected date in the pickerAbstractField.setValue(Object),
setLabel(String)public DatePicker(HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>> listener)
ValueChangeListener.listener - the listener to receive value change eventsAbstractField.addValueChangeListener(HasValue.ValueChangeListener)public DatePicker(String label, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>> listener)
ValueChangeListener and a label.label - the label describing the date pickerlistener - the listener to receive value change eventssetLabel(String),
AbstractField.addValueChangeListener(HasValue.ValueChangeListener)public DatePicker(LocalDate initialDate, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>> listener)
ValueChangeListener.initialDate - the pre-selected date in the pickerlistener - the listener to receive value change eventsAbstractField.setValue(Object),
AbstractField.addValueChangeListener(HasValue.ValueChangeListener)public DatePicker(String label, LocalDate initialDate, HasValue.ValueChangeListener<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>> listener)
ValueChangeListener and a label.label - the label describing the date pickerinitialDate - the pre-selected date in the pickerlistener - the listener to receive value change eventssetLabel(String),
AbstractField.setValue(Object),
AbstractField.addValueChangeListener(HasValue.ValueChangeListener)public void setMin(LocalDate min)
min - the minimum date that is allowed to be selected, or
null to remove any minimum constraintspublic LocalDate getMin()
null if there's no minimumpublic void setMax(LocalDate max)
max - the maximum date that is allowed to be selected, or
null to remove any maximum constraintspublic LocalDate getMax()
null if there's no maximumpublic void setLocale(Locale locale)
NOTE:Supported formats are MM/DD/YYYY, DD/MM/YYYY and YYYY/MM/DD. Browser compatibility can be different based on the browser and mobile devices, you can check here for more details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
When using custom date formats through
setI18n(DatePickerI18n), setting a locale has no
effect, and dates will always be parsed and displayed using the custom
date format.
locale - the locale set to the date picker, cannot be nullpublic Locale getLocale()
protected void onAttach(AttachEvent attachEvent)
ComponentThe default implementation does nothing.
This method is invoked before the AttachEvent is fired for the
component.
public DatePicker.DatePickerI18n getI18n()
Note: updating the object content that is gotten from this method will
not update the lang on the component if not set back using
setI18n(DatePickerI18n)
null, If the i18n
properties weren't set.public void setI18n(DatePicker.DatePickerI18n i18n)
i18n - the internationalized properties, not nullpublic void setErrorMessage(String errorMessage)
GeneratedVaadinDatePickerDescription copied from corresponding location in WebComponent:
The error message to display when the input is invalid.
setErrorMessage in interface HasValidationsetErrorMessage in class GeneratedVaadinDatePicker<DatePicker,LocalDate>errorMessage - the String value to setpublic String getErrorMessage()
getErrorMessage in interface HasValidationpublic Validator<LocalDate> getDefaultValidator()
HasValidatorBinder and
can be seen as a validation failure.getDefaultValidator in interface HasValidator<LocalDate>public Registration addValidationStatusChangeListener(ValidationStatusChangeListener<LocalDate> 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<LocalDate>Binder.BindingBuilderImpl.bind(ValueProvider,
Setter)public void setInvalid(boolean invalid)
GeneratedVaadinDatePickerDescription copied from corresponding location in WebComponent:
This property is set to true when the control value invalid.
setInvalid in interface HasValidationsetInvalid in class GeneratedVaadinDatePicker<DatePicker,LocalDate>invalid - the boolean value to setpublic boolean isInvalid()
return true, if the value is invalid.
isInvalid in interface HasValidationvalidity property from the datepickerpublic void setValue(LocalDate value)
HasValuegetValue(), fires a value change event. May throw
IllegalArgumentException 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 not null.
setValue in interface HasValue<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>,LocalDate>setValue in class AbstractField<DatePicker,LocalDate>value - the new valuepublic void setClearButtonVisible(boolean clearButtonVisible)
The clear button is an icon, which can be clicked to set the datepicker
value to null.
setClearButtonVisible in class GeneratedVaadinDatePicker<DatePicker,LocalDate>clearButtonVisible - true to display the clear button, false to
hide itpublic boolean isClearButtonVisible()
true if this datepicker displays a clear button,
false otherwisesetClearButtonVisible(boolean)public void setLabel(String label)
setLabel in interface HasLabelsetLabel in class GeneratedVaadinDatePicker<DatePicker,LocalDate>label - value for the label property in the datepickerpublic String getLabel()
public void setPlaceholder(String placeholder)
GeneratedVaadinDatePickerDescription copied from corresponding location in WebComponent:
A placeholder string in addition to the label. If this is set, the label will always float.
setPlaceholder in class GeneratedVaadinDatePicker<DatePicker,LocalDate>placeholder - the String value to setpublic String getPlaceholder()
This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
placeholder property of the datePickerpublic void setInitialPosition(LocalDate initialPosition)
The same date formats as for the value property are supported.
initialPosition - the LocalDate value to setpublic LocalDate getInitialPosition()
The same date formats as for the value property are supported.
This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
initialPosition property from the datepickerpublic void setRequired(boolean required)
GeneratedVaadinDatePickerDescription copied from corresponding location in WebComponent:
Set to true to mark the input as required.
setRequired in class GeneratedVaadinDatePicker<DatePicker,LocalDate>required - the boolean value to setpublic void setRequiredIndicatorVisible(boolean required)
HasValueIf 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.
setRequiredIndicatorVisible in interface HasValue<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>,LocalDate>setRequiredIndicatorVisible in interface HasValueAndElement<AbstractField.ComponentValueChangeEvent<DatePicker,LocalDate>,LocalDate>required - true to make the required indicator visible,
false if notpublic boolean isRequired()
This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
true if the input is required, false otherwisepublic void setWeekNumbersVisible(boolean weekNumbersVisible)
Set true to display ISO-8601 week numbers in the calendar.
Notice that displaying week numbers is only supported when i18n.firstDayOfWeek is 1 (Monday).
weekNumbersVisible - the boolean value to setpublic boolean isWeekNumbersVisible()
showWeekNumbers property of the datepicker
This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.
showWeekNumbers property from the datepickerpublic void setOpened(boolean opened)
setOpened in class GeneratedVaadinDatePicker<DatePicker,LocalDate>opened - true to open the datepicker overlay, false to
close itpublic void open()
open in class GeneratedVaadinDatePicker<DatePicker,LocalDate>protected void close()
close in class GeneratedVaadinDatePicker<DatePicker,LocalDate>public boolean isOpened()
true if the drop-down is opened, false otherwisepublic void setName(String name)
GeneratedVaadinDatePickerDescription copied from corresponding location in WebComponent:
The name of this element.
setName in class GeneratedVaadinDatePicker<DatePicker,LocalDate>name - the String value to setpublic String getName()
name property from the DatePickerpublic void setAutoOpen(boolean autoOpen)
autoOpen - Value for the auto open property,public boolean isAutoOpen()
true if auto open is enabled. false otherwise.
Default is trueprotected void validate()
public Registration addOpenedChangeListener(ComponentEventListener<GeneratedVaadinDatePicker.OpenedChangeEvent<DatePicker>> listener)
GeneratedVaadinDatePickeropened-changed events fired by the
webcomponent.addOpenedChangeListener in class GeneratedVaadinDatePicker<DatePicker,LocalDate>listener - the listenerRegistration for removing the event listenerpublic Registration addInvalidChangeListener(ComponentEventListener<GeneratedVaadinDatePicker.InvalidChangeEvent<DatePicker>> listener)
GeneratedVaadinDatePickerinvalid-changed events fired by the
webcomponent.addInvalidChangeListener in class GeneratedVaadinDatePicker<DatePicker,LocalDate>listener - the listenerRegistration for removing the event listenerprotected boolean isEnforcedFieldValidationEnabled()
Exposed with protected visibility to support mocking
The method requires the VaadinSession instance to obtain the
application configuration properties, otherwise, the feature is
considered disabled.
true if enabled, false otherwise.Copyright © 2025. All rights reserved.