答案 0 :(得分:0)
下面的代码是将DatePicker的Date Rage从Jan2000限制为tillDate。
restrictDatePicker(datePicker,LocalDate.of(2000,Month.JANUARY,1),LocalDate.now());
我们可以根据要求更改最小和最大日期值。
public void restrictDatePicker(DatePicker datePicker, LocalDate minDate, LocalDate maxDate) {
final Callback<DatePicker, DateCell> dayCellFactory = new Callback<DatePicker, DateCell>() {
@Override
public DateCell call(final DatePicker datePicker) {
return new DateCell() {
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
if (item.isBefore(minDate)) {
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}else if (item.isAfter(maxDate)) {
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}
}
};
}
};
datePicker.setDayCellFactory(dayCellFactory);
}
参考链接:https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/date-picker.htm#CCHEBIFF