Java Reference
In-Depth Information
How It Works
For a number of years, the JavaFX platform did not have a standard
DatePicker
component. The
DatePicker
component, new to JavaFX 8, fills that void by provid-
ing developers with a way to select a date from a given calendar. The
DatePicker
component goes hand-in-hand with the Date-Time API, which enables easy date selec-
tion and date calculations.
The
DatePicker
includes a Month selector, Year selector, and Day selector. To
add the component to your application, import
javafx.scene.control.DatePicker
and create a new instance of the com-
ponent. By default, if no date is passed into the component it will display a blank text
box. The constructor for the component allows you to pass in a
LocalDate
object
upon initialization, as shown in the example.
The
DatePicker
is very extensible, as it provides the ability for customization.
For instance, the weeks of the year can be set using the
setShowWeekNumbers()
method, and the chronology can be set using the
setChronology()
method. The
following code shows the same example from the solution, but this time the
DatePicker
has been customized to show the week numbers and use Japanese chro-
nology.
public void start(Stage primaryStage) throws Exception {
dateLabel = new Label("Select a date using the
widget");
datePicker = new DatePicker(LocalDate.now());
datePicker.setOnAction(event -> {
dateLabel.setText("The selected date is: "
+ datePicker.getValue());
});
datePicker.setShowWeekNumbers(true);
datePicker.setChronology(JapaneseChronology.INSTANCE);
FlowPane flow = new FlowPane();
flow.setPadding(new Insets(5, 5, 5, 5));
flow.getChildren().add(dateLabel);
flow.getChildren().add(datePicker);
primaryStage.setScene(new Scene(flow, 300, 100));