import java.time.LocalDate; import java.time.chrono.Chronology; import java.time.format.DateTimeFormatter; package javafx.scene.control; /** * The DatePicker control allows the user to enter a date as text or * to select a date from a calendar popup. The calendar is based on * either the standard ISO-8601 chronology or any of the other * chronology classes defined in the java.time.chrono package. * *

The {@link #valueProperty() value} property represents the * currently selected {@link java.time.LocalDate}. An initial date can * be set via the {@link #DatePicker(java.time.LocalDate) constructor} * or by calling {@link #setValue(java.time.LocalDate) setValue()}. 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 {@link #chronologyProperty() chronology} property specifies a * calendar system to be used for parsing, displaying, and choosing * dates. * The {@link #valueProperty() 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 * {@link java.time.chrono.Chronology} API to get or set the * corresponding {@link java.time.chrono.ChronoLocalDate} value. For * example: * *

 * LocalDate isoDate = datePicker.getValue();
 * ChronoLocalDate chronoDate =
 *     ((isoDate != null) ? datePicker.getChronology().date(isoDate) : null);
 * System.err.println("Selected date: " + chronoDate);
 * 
* * * @since 8.0 */ public class DatePicker extends ComboBoxBase { /** * Creates a default DatePicker instance with no date value set. */ public DatePicker(); /** * Creates a DatePicker instance and sets the * {@link #valueProperty() value} to the given date. * * @param localDate to be set as the currently selected date in the DatePicker. */ public DatePicker(LocalDate localDate); /** * A custom cell factory can be provided to customize individual * day cells in the DatePicker popup. Refer to {@link DateCell} * and {@link Cell} for more information on cell factories. * Example: * *

     * final Callback dayCellFactory = new Callback() {
     *     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);
     * 
*/ public final void setDayCellFactory(Callback value); public final Callback getDayCellFactory(); public final ObjectProperty> dayCellFactoryProperty(); /** * The calendar system used for parsing, displaying, and choosing * dates in the DatePicker control. * *

The default value is returned from a call to * {@code Chronology.ofLocale(Locale.getDefault(Locale.Category.FORMAT))}. * The default is usually {@link java.time.chrono.IsoChronology} unless * provided explicitly in the {@link java.util.Locale} by use of a * Locale calendar extension. */ public ObjectProperty chronologyProperty(); public final Chronology getChronology(); public final void setChronology(Chronology value); /** * Whether the DatePicker popup should display a column showing * week numbers. * *

The default value is false unless otherwise defined in a * resource bundle for the current locale. * *

This property may be toggled by the end user by using a * context menu in the DatePicker popup, so it is recommended that * applications save and restore the value between sessions. */ public final BooleanProperty showWeekNumbersProperty(); public final void setShowWeekNumbers(boolean value); public final boolean isShowWeekNumbers(); /** * Converts the input text to an object of type LocalDate and vice * versa. * *

If not set by the application, the DatePicker skin class will * set a converter based on a {@link java.time.DateTimeFormatter} * for the current {@link java.util.Locale} and * {@link #chronologyProperty() chronology}. This formatter is * then used to parse and display the current date value. * *

Example using an explicit formatter: *


     * datePicker.setConverter(new StringConverter() {
     *     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 defaultConverter = datePicker.getConverter();
     *   datePicker.setConverter(new StringConverter() {
     *       @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());
     *               return null;
     *           }
     *       }
     *   });
     * 
* * @see javafx.scene.control.ComboBox#converterProperty */ public ObjectProperty> converterProperty(); public final void setConverter(StringConverter value); public final StringConverter getConverter(); /** * The editor for the DatePicker. * * @see javafx.scene.control.ComboBox#editorProperty */ public final TextField getEditor(); public final ReadOnlyObjectProperty editorProperty(); } ---- /** * DateCell is used by {@link DatePicker} to render the individual * grid cells in the calendar month. By providing a * {@link DatePicker#dayCellFactoryProperty() dayCellFactory}, an * application can provide an update method to change each cell's * properties such as text, background color, etc. * * @since 8.0 */ public class DateCell extends Cell { /** {@inheritDoc} */ @Override public void updateItem(LocalDate item, boolean empty); /** {@inheritDoc} */ @Override protected Skin createDefaultSkin(); }