Tech and Media Labs
This site uses cookies to improve the user experience.




JavaFX DatePicker

Jakob Jenkov
Last update: 2016-05-18

A JavaFX DatePicker control enables the user to enter a date or choose a date from a wizard-like popup dialog. The popup dialog shows only valid dates, so this is an easier way for users to choose a date and ensure that both the date and date format entered in the date picker text field is valid. The JavaFX DatePicker is represented by the class javafx.scene.control.DatePicker .

The DatePicker is a subclass of the ComboBox class, and thus shares some similarities with this class.

Creating a DatePicker

You create a DatePicker control via the constructor of the DatePicker class. Here is a JavaFX DatePicker instantiation example:

DatePicker datePicker = new DatePicker();

Adding a DatePicker to the Scene Graph

To make a DatePicker visible it must be added to the JavaFX scene graph. This means adding it to a Scene object, or to a layout component which is added to a Scene object.

Here is an example showing how to add a JavaFX DatePicker to the scene graph:

package com.jenkov.javafx.controls;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class DatePickerExperiments extends Application  {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Button Experiment 1");

        DatePicker datePicker = new DatePicker();

        HBox hbox = new HBox(datePicker);

        Scene scene = new Scene(hbox, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

The application resulting from running this example would look similar to this:

A JavaFX DatePicker displayed in the scene graph of a JavaFX application.

Reading the Selected Date

Reading the date selected in the DatePicker can be done using its getValue() method. Here is an example of reading the selected date from a DatePicker:

LocalDate value = datePicker.getValue();

The getValue() returns a LocalDate object representing the date selected in the DatePicker.

Here is a full example with a button added to extract the selected date in the DatePicker when the button is clicked:

package com.jenkov.javafx.controls;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.time.LocalDate;


public class DatePickerExperiments extends Application  {


    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("DatePicker Experiment 1");

        DatePicker datePicker = new DatePicker();

        Button button = new Button("Read Date");

        button.setOnAction(action -> {
            LocalDate value = datePicker.getValue();
        });

        HBox hbox = new HBox(datePicker);

        Scene scene = new Scene(hbox, 300, 240);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC