Datepicker in JavaFX 8


Question

Is there any implementation of Date Picker and Time Picker into the default JavaFX 8 package that I can use without using third party solutions?

1
5
12/4/2013 7:24:28 AM

Accepted Answer

DatePicker

Yes, Java 8 has a DatePicker:

datepicker

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

import java.time.LocalDate;

public class PickerDemo extends Application {
    @Override public void start(Stage stage) {
        final DatePicker datePicker = new DatePicker(LocalDate.now());
        datePicker.setOnAction(event -> {
            LocalDate date = datePicker.getValue();
            System.out.println("Selected date: " + date);
        });

        stage.setScene(
            new Scene(datePicker)
        );
        stage.show();
    }
    public static void main(String[] args) { launch(args); }
}

TimePicker

No, Java 8 does not have a TimePicker.

There is a TimePicker in jfxtras (source here).

Given that Java 8 already has a DatePicker, the addition of a TimePicker might be an appropriate feature request, you could make.

8
12/4/2013 8:21:08 AM

If you do not want to display calendar in popup. Here is a solution which uses internal CalendarPickerContent class.

DatePickerSkin skin = new DatePickerSkin(new DatePicker());
Node calendarControl = skin.getPopupContent();

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon