Govind_das
Govind_das

Reputation: 67

JavaFX DatePicker disable future dates

I want to allow user to select date from DateChooser upto current date. How can I disable future dates in DatePicker of JavaFX?

Upvotes: 3

Views: 1856

Answers (1)

matt_frangakis
matt_frangakis

Reputation: 193

You should set a DayCellFactory. This allows you to control essentially all style elements of the datepicker, including whether or not dates after a specified date are greyed out.

datePicker.setDayCellFactory(param -> new DateCell() {
        @Override
        public void updateItem(LocalDate date, boolean empty) {
            super.updateItem(date, empty);
            setDisable(empty || date.compareTo(LocalDate.now()) > 0 );
        }
    });

PS: any node that has cells in it (TableView, DatePicker, ListView etc) can have its updateItem method overridden, which allows you to configure the style of that cell based on its data. Use with caution, and always make sure you include the super.updateItem() that comes with it.

Upvotes: 5

Related Questions