2016-03-10 105 views

回答

0

它可以限制可供用戶選擇通過禁止在dayCellFactory那些日子裏,並設置這些日期範圍內您的DatePicker日期,官方文檔可以發現here,這裏有一個例子:

DatePicker myDatePicker = new DatePicker(); // This DatePicker is shown to user 
DatePicker maxDate = new DatePicker(); // DatePicker, used to define max date available, you can also create another for minimum date 
maxDate.setValue(LocalDate.of(2015, Month.JANUARY, 1)); // Max date available will be 2015-01-01 
final Callback<DatePicker, DateCell> dayCellFactory; 

dayCellFactory = (final DatePicker datePicker) -> new DateCell() { 
    @Override 
    public void updateItem(LocalDate item, boolean empty) { 
     super.updateItem(item, empty); 
     if (item.isAfter(maxDate.getValue())) { //Disable all dates after required date 
      setDisable(true); 
      setStyle("-fx-background-color: #ffc0cb;"); //To set background on different color 
     } 
    } 
}; 
//Finally, we just need to update our DatePicker cell factory as follow: 
myDatePicker.setDayCellFactory(dayCellFactory); 

現在myDatePicker將不允許用戶在2015-01-01之後選擇日期(記住,日期將顯示但不可用於選擇),在這裏您還可以創建另一個臨時日期選擇器來設置可用日期範圍的最短日期這些代碼的方式必須放在java控制器的初始化方法上

相關問題