public class DatePicker extends ComboBoxBase<java.time.LocalDate>
The value
property represents the
currently selected LocalDate
. An initial date can
be set via the constructor
or by calling setValue(LocalDate)
. The
default value is null.
final DatePicker datePicker = new DatePicker();
datePicker.setOnAction(new EventHandler() {
public void handle(Event t) {
LocalDate date = datePicker.getValue();
System.err.println("Selected date: " + date);
}
});
The chronology
property specifies a
calendar system to be used for parsing, displaying, and choosing
dates.
The value
property is always defined in
the ISO calendar system, however, so applications based on a
different chronology may use the conversion methods provided in the
Chronology
API to get or set the
corresponding ChronoLocalDate
value. For
example:
LocalDate isoDate = datePicker.getValue();
ChronoLocalDate chronoDate =
((isoDate != null) ? datePicker.getChronology().date(isoDate) : null);
System.err.println("Selected date: " + chronoDate);
Type | Property and Description |
---|---|
<any> |
chronology
The calendar system used for parsing, displaying, and choosing
dates in the DatePicker control.
|
<any> |
converter
Converts the input text to an object of type LocalDate and vice
versa.
|
<any> |
dayCellFactory
A custom cell factory can be provided to customize individual
day cells in the DatePicker popup.
|
<any> |
editor
The editor for the DatePicker.
|
BooleanProperty |
showWeekNumbers
Whether the DatePicker popup should display a column showing
week numbers.
|
armedProperty, editableProperty, onActionProperty, onHiddenProperty, onHidingProperty, onShowingProperty, onShownProperty, promptTextProperty, showingProperty, valueProperty
contextMenuProperty, skinClassNameProperty, skinProperty, tooltipProperty
Modifier and Type | Class and Description |
---|---|
private static class |
DatePicker.StyleableProperties |
Modifier and Type | Field and Description |
---|---|
private <any> |
chronology |
private <any> |
converter |
private <any> |
dayCellFactory
A custom cell factory can be provided to customize individual
day cells in the DatePicker popup.
|
private static java.lang.String |
DEFAULT_STYLE_CLASS
*
Stylesheet Handling *
*
|
private <any> |
defaultConverter |
private <any> |
editor
The editor for the DatePicker.
|
private java.time.chrono.Chronology |
lastValidChronology |
private java.time.LocalDate |
lastValidDate |
private BooleanProperty |
showWeekNumbers |
ON_HIDDEN, ON_HIDING, ON_SHOWING, ON_SHOWN
Constructor and Description |
---|
DatePicker()
Creates a default DatePicker instance with a
null date value set. |
DatePicker(java.time.LocalDate localDate)
Creates a DatePicker instance and sets the
value to the given date. |
Modifier and Type | Method and Description |
---|---|
<any> |
chronologyProperty()
The calendar system used for parsing, displaying, and choosing
dates in the DatePicker control.
|
<any> |
converterProperty()
Converts the input text to an object of type LocalDate and vice
versa.
|
protected Skin<?> |
createDefaultSkin()
Create a new instance of the default skin for this control.
|
<any> |
dayCellFactoryProperty()
A custom cell factory can be provided to customize individual
day cells in the DatePicker popup.
|
<any> |
editorProperty()
The editor for the DatePicker.
|
java.time.chrono.Chronology |
getChronology()
Gets the value of the property chronology.
|
static java.util.List<<any>> |
getClassCssMetaData() |
java.util.List<<any>> |
getControlCssMetaData() |
<any> |
getConverter()
Gets the value of the property converter.
|
<any> |
getDayCellFactory()
Gets the value of the property dayCellFactory.
|
TextField |
getEditor()
Gets the value of the property editor.
|
boolean |
isShowWeekNumbers()
Gets the value of the property showWeekNumbers.
|
java.lang.Object |
queryAccessibleAttribute(AccessibleAttribute attribute,
java.lang.Object... parameters) |
void |
setChronology(java.time.chrono.Chronology value)
Sets the value of the property chronology.
|
void |
setConverter(<any> value)
Sets the value of the property converter.
|
void |
setDayCellFactory(<any> value)
Sets the value of the property dayCellFactory.
|
void |
setShowWeekNumbers(boolean value)
Sets the value of the property showWeekNumbers.
|
BooleanProperty |
showWeekNumbersProperty()
Whether the DatePicker popup should display a column showing
week numbers.
|
private boolean |
validateDate(java.time.chrono.Chronology chrono,
java.time.LocalDate date) |
arm, armedProperty, disarm, editableProperty, executeAccessibleAction, getOnAction, getOnHidden, getOnHiding, getOnShowing, getOnShown, getPromptText, getValue, hide, isArmed, isEditable, isShowing, onActionProperty, onHiddenProperty, onHidingProperty, onShowingProperty, onShownProperty, promptTextProperty, setEditable, setOnAction, setOnHidden, setOnHiding, setOnShowing, setOnShown, setPromptText, setValue, show, showingProperty, valueProperty
computeMaxHeight, computeMaxWidth, computeMinHeight, computeMinWidth, computePrefHeight, computePrefWidth, contextMenuProperty, getBaselineOffset, getContextMenu, getControlChildren, getCssMetaData, getInitialFocusTraversable, getSkin, getTooltip, isResizable, layoutChildren, loadSkinClass, setContextMenu, setSkin, setTooltip, skinClassNameProperty, skinProperty, tooltipProperty
public final <any> dayCellFactoryProperty
DateCell
and Cell
for more information on cell factories.
Example:
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
public DateCell call(final DatePicker datePicker) {
return new DateCell() {
@Override public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
if (MonthDay.from(item).equals(MonthDay.of(9, 25))) {
setTooltip(new Tooltip("Happy Birthday!"));
setStyle("-fx-background-color: #ff4444;");
}
if (item.equals(LocalDate.now().plusDays(1))) {
// Tomorrow is too soon.
setDisable(true);
}
}
};
}
};
datePicker.setDayCellFactory(dayCellFactory);
getDayCellFactory()
,
#setDayCellFactory()
public final <any> chronologyProperty
The default value is returned from a call to
Chronology.ofLocale(Locale.getDefault(Locale.Category.FORMAT))
.
The default is usually IsoChronology
unless
provided explicitly in the Locale
by use of a
Locale calendar extension.
Setting the value to null
will restore the default
chronology.
getChronology()
,
setChronology(Chronology)
public final BooleanProperty showWeekNumbersProperty
The default value is specified in a resource bundle, and depends on the country of the current locale.
isShowWeekNumbers()
,
setShowWeekNumbers(boolean)
public final <any> converterProperty
If not set by the application, the DatePicker skin class will
set a converter based on a DateTimeFormatter
for the current Locale
and
chronology
. This formatter is
then used to parse and display the current date value.
Setting the value to null
will restore the default
converter.
Example using an explicit formatter:
datePicker.setConverter(new StringConverter<LocalDate>() {
String pattern = "yyyy-MM-dd";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
{
datePicker.setPromptText(pattern.toLowerCase());
}
@Override public String toString(LocalDate date) {
if (date != null) {
return dateFormatter.format(date);
} else {
return "";
}
}
@Override public LocalDate fromString(String string) {
if (string != null && !string.isEmpty()) {
return LocalDate.parse(string, dateFormatter);
} else {
return null;
}
}
});
Example that wraps the default formatter and catches parse exceptions:
final StringConverter<LocalDate> defaultConverter = datePicker.getConverter();
datePicker.setConverter(new StringConverter<LocalDate>() {
@Override public String toString(LocalDate value) {
return defaultConverter.toString(value);
}
@Override public LocalDate fromString(String text) {
try {
return defaultConverter.fromString(text);
} catch (DateTimeParseException ex) {
System.err.println("HelloDatePicker: "+ex.getMessage());
throw ex;
}
}
});
The default base year for parsing input containing only two digits for
the year is 2000 (see DateTimeFormatter
). This
default is not useful for allowing a person's date of birth to be typed.
The following example modifies the converter's fromString() method to
allow a two digit year for birth dates up to 99 years in the past.
@Override public LocalDate fromString(String text) {
if (text != null && !text.isEmpty()) {
Locale locale = Locale.getDefault(Locale.Category.FORMAT);
Chronology chrono = datePicker.getChronology();
String pattern =
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT,
null, chrono, locale);
String prePattern = pattern.substring(0, pattern.indexOf("y"));
String postPattern = pattern.substring(pattern.lastIndexOf("y")+1);
int baseYear = LocalDate.now().getYear() - 99;
DateTimeFormatter df = new DateTimeFormatterBuilder()
.parseLenient()
.appendPattern(prePattern)
.appendValueReduced(ChronoField.YEAR, 2, 2, baseYear)
.appendPattern(postPattern)
.toFormatter();
return LocalDate.from(chrono.date(df.parse(text)));
} else {
return null;
}
}
getConverter()
,
#setConverter()
public final <any> editorProperty
getEditor()
private java.time.LocalDate lastValidDate
private java.time.chrono.Chronology lastValidChronology
private <any> dayCellFactory
DateCell
and Cell
for more information on cell factories.
Example:
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
public DateCell call(final DatePicker datePicker) {
return new DateCell() {
@Override public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
if (MonthDay.from(item).equals(MonthDay.of(9, 25))) {
setTooltip(new Tooltip("Happy Birthday!"));
setStyle("-fx-background-color: #ff4444;");
}
if (item.equals(LocalDate.now().plusDays(1))) {
// Tomorrow is too soon.
setDisable(true);
}
}
};
}
};
datePicker.setDayCellFactory(dayCellFactory);
private <any> chronology
private BooleanProperty showWeekNumbers
private <any> converter
private <any> defaultConverter
private <any> editor
ComboBox.editorProperty()
private static final java.lang.String DEFAULT_STYLE_CLASS
public DatePicker()
null
date value set.public DatePicker(java.time.LocalDate localDate)
value
to the given date.localDate
- to be set as the currently selected date in the DatePicker. Can be null.private boolean validateDate(java.time.chrono.Chronology chrono, java.time.LocalDate date)
public final void setDayCellFactory(<any> value)
public final <any> getDayCellFactory()
public final <any> dayCellFactoryProperty()
DateCell
and Cell
for more information on cell factories.
Example:
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
public DateCell call(final DatePicker datePicker) {
return new DateCell() {
@Override public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
if (MonthDay.from(item).equals(MonthDay.of(9, 25))) {
setTooltip(new Tooltip("Happy Birthday!"));
setStyle("-fx-background-color: #ff4444;");
}
if (item.equals(LocalDate.now().plusDays(1))) {
// Tomorrow is too soon.
setDisable(true);
}
}
};
}
};
datePicker.setDayCellFactory(dayCellFactory);
getDayCellFactory()
,
#setDayCellFactory()
public final <any> chronologyProperty()
The default value is returned from a call to
Chronology.ofLocale(Locale.getDefault(Locale.Category.FORMAT))
.
The default is usually IsoChronology
unless
provided explicitly in the Locale
by use of a
Locale calendar extension.
Setting the value to null
will restore the default
chronology.
getChronology()
,
setChronology(Chronology)
public final java.time.chrono.Chronology getChronology()
public final void setChronology(java.time.chrono.Chronology value)
public final BooleanProperty showWeekNumbersProperty()
The default value is specified in a resource bundle, and depends on the country of the current locale.
isShowWeekNumbers()
,
setShowWeekNumbers(boolean)
public final void setShowWeekNumbers(boolean value)
public final boolean isShowWeekNumbers()
public final <any> converterProperty()
If not set by the application, the DatePicker skin class will
set a converter based on a DateTimeFormatter
for the current Locale
and
chronology
. This formatter is
then used to parse and display the current date value.
Setting the value to null
will restore the default
converter.
Example using an explicit formatter:
datePicker.setConverter(new StringConverter<LocalDate>() {
String pattern = "yyyy-MM-dd";
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
{
datePicker.setPromptText(pattern.toLowerCase());
}
@Override public String toString(LocalDate date) {
if (date != null) {
return dateFormatter.format(date);
} else {
return "";
}
}
@Override public LocalDate fromString(String string) {
if (string != null && !string.isEmpty()) {
return LocalDate.parse(string, dateFormatter);
} else {
return null;
}
}
});
Example that wraps the default formatter and catches parse exceptions:
final StringConverter<LocalDate> defaultConverter = datePicker.getConverter();
datePicker.setConverter(new StringConverter<LocalDate>() {
@Override public String toString(LocalDate value) {
return defaultConverter.toString(value);
}
@Override public LocalDate fromString(String text) {
try {
return defaultConverter.fromString(text);
} catch (DateTimeParseException ex) {
System.err.println("HelloDatePicker: "+ex.getMessage());
throw ex;
}
}
});
The default base year for parsing input containing only two digits for
the year is 2000 (see DateTimeFormatter
). This
default is not useful for allowing a person's date of birth to be typed.
The following example modifies the converter's fromString() method to
allow a two digit year for birth dates up to 99 years in the past.
@Override public LocalDate fromString(String text) {
if (text != null && !text.isEmpty()) {
Locale locale = Locale.getDefault(Locale.Category.FORMAT);
Chronology chrono = datePicker.getChronology();
String pattern =
DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.SHORT,
null, chrono, locale);
String prePattern = pattern.substring(0, pattern.indexOf("y"));
String postPattern = pattern.substring(pattern.lastIndexOf("y")+1);
int baseYear = LocalDate.now().getYear() - 99;
DateTimeFormatter df = new DateTimeFormatterBuilder()
.parseLenient()
.appendPattern(prePattern)
.appendValueReduced(ChronoField.YEAR, 2, 2, baseYear)
.appendPattern(postPattern)
.toFormatter();
return LocalDate.from(chrono.date(df.parse(text)));
} else {
return null;
}
}
getConverter()
,
#setConverter()
public final void setConverter(<any> value)
public final <any> getConverter()
public final TextField getEditor()
public final <any> editorProperty()
getEditor()
protected Skin<?> createDefaultSkin()
-fx-skin
or set explicitly in a sub-class with setSkin(...)
.createDefaultSkin
in class Control
public static java.util.List<<any>> getClassCssMetaData()
public java.util.List<<any>> getControlCssMetaData()
getControlCssMetaData
in class Control
public java.lang.Object queryAccessibleAttribute(AccessibleAttribute attribute, java.lang.Object... parameters)
queryAccessibleAttribute
in class ComboBoxBase<java.time.LocalDate>