Example usage for javafx.scene.control DatePicker setConverter

List of usage examples for javafx.scene.control DatePicker setConverter

Introduction

In this page you can find the example usage for javafx.scene.control DatePicker setConverter.

Prototype

public final void setConverter(StringConverter<LocalDate> value) 

Source Link

Usage

From source file:Main.java

@Override
public void start(Stage stage) {
    String pattern = "yyyy-MM-dd";
    VBox vbox = new VBox(20);
    Scene scene = new Scene(vbox, 400, 400);
    stage.setScene(scene);/* w ww.  j  a va2 s.c  om*/
    DatePicker checkInDatePicker = new DatePicker();
    StringConverter<LocalDate> converter = new StringConverter<LocalDate>() {
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);

        @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;
            }
        }
    };
    checkInDatePicker.setConverter(converter);
    checkInDatePicker.setPromptText(pattern.toLowerCase());

    vbox.getChildren().add(checkInDatePicker);
    checkInDatePicker.requestFocus();
    stage.show();
}