NULL
NULL

Reputation: 61

How to customized a Date Pickers' calendar pop up to show on a specific date rather than the current date

Basically, when creating a new DatePicker(), whenever you click on the calendar button, the default calendar pop-up will point directly to the current date. Is there a way to customize the pop-up so that it will point to a specific date (e.g. Last year, or the following year, or on the 1st day of a non-disabled date on the DatePicker)?

Here is a sample image using JFXDatePicker, but instead of the current date, I want to show the date 26 years ago.

default output

Expected Output:

expected output

Upvotes: 0

Views: 90

Answers (1)

NULL
NULL

Reputation: 61

Based on this answer, I came up with a simple solution (not perfect but still solves my question).

    @Override
    public void start(Stage primaryStage) {
        JFXDatePicker jfxDatePicker = new JFXDatePicker();
        jfxDatePicker.showingProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
                if (t1 && jfxDatePicker.getValue() == null) {
                    jfxDatePicker.setValue(LocalDate.now().minusYears(26));
                    Platform.runLater(()->{
                        jfxDatePicker.getEditor().clear();
                    });
                }
            }
        });

        VBox root = new VBox(25, jfxDatePicker);
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root, 300, 120);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

(Open for suggestions and improvements).

Upvotes: 1

Related Questions