javafx datepicker如何自定义 [英] javafx datepicker how to customize
问题描述
我有一个日期选择器的简单代码,它禁止所有日期之前的选择,但我需要能够禁用其他日期(例如:17.10.2014至19.10.2014)。我如何以特定日期也被禁用的方式进行更改?
i have the simple code for date picker which disables all the dates which are before than the choosen one,but i need to be able to disable other dates as well (like for example:17.10.2014 until 19.10.2014).How could i change it in the way that specific dates are also disabled ?
public class DatePickerSample extends Application {
public class DatePickerSample extends Application {
private Stage stage;
private DatePicker checkInDatePicker;
private DatePicker checkOutDatePicker;
public static void main(String[] args) {
Locale.setDefault(Locale.US);
launch(args);
}
@Override
public void start(Stage stage) {
this.stage = stage;
stage.setTitle("DatePickerSample ");
initUI();
stage.show();
}
private void initUI() {
VBox vbox = new VBox(20);
vbox.setStyle("-fx-padding: 10;");
Scene scene = new Scene(vbox, 400, 400);
stage.setScene(scene);
checkInDatePicker = new DatePicker();
checkOutDatePicker = new DatePicker();
checkInDatePicker.setValue(LocalDate.now());
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(
checkInDatePicker.getValue().plusDays(1))
) {
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}
long p = ChronoUnit.DAYS.between(
checkInDatePicker.getValue(), item
);
setTooltip(new Tooltip(
"You're about to stay for " + p + " days")
);
}
};
}
};
checkOutDatePicker.setDayCellFactory(dayCellFactory);
checkOutDatePicker.setValue(checkInDatePicker.getValue().plusDays(1));
GridPane gridPane = new GridPane();
gridPane.setHgap(10);
gridPane.setVgap(10);
Label checkInlabel = new Label("Check-In Date:");
gridPane.add(checkInlabel, 0, 0);
GridPane.setHalignment(checkInlabel, HPos.LEFT);
gridPane.add(checkInDatePicker, 0, 1);
Label checkOutlabel = new Label("Check-Out Date:");
gridPane.add(checkOutlabel, 0, 2);
GridPane.setHalignment(checkOutlabel, HPos.LEFT);
gridPane.add(checkOutDatePicker, 0, 3);
vbox.getChildren().add(gridPane);
}
}
推荐答案
如果要禁用多个日期范围,可以创建此POJO:
If you want to have several ranges of dates to disable, you can create this POJO:
class DisabledRange {
private final LocalDate initialDate;
private final LocalDate endDate;
public DisabledRange(LocalDate initialDate, LocalDate endDate){
this.initialDate=initialDate;
this.endDate = endDate;
}
public LocalDate getInitialDate() { return initialDate; }
public LocalDate getEndDate() { return endDate; }
}
现在你可以定义一个范围集合在您的日历中禁用例如:
And now in you can define a collection of ranges to disable in your calendar. For instance:
private final ObservableList<DisabledRange> rangesToDisable =
FXCollections.observableArrayList(
new DisabledRange(LocalDate.of(2014,10,17), LocalDate.of(2014,10,19)),
new DisabledRange(LocalDate.of(2014,10,27), LocalDate.of(2014,10,29)));
最后,您只需要检入回调
如果项目
在以下范围内:
Finally, you just need to check in the Callback
if the item
is within any of these ranges:
@Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
boolean disable = rangesToDisable.stream()
.filter(r->r.initialDate.minusDays(1).isBefore(item))
.filter(r->r.endDate.plusDays(1).isAfter(item))
.findAny()
.isPresent();
if (item.isBefore(checkInDatePicker.getValue().plusDays(1)) ||
disable) {
setDisable(true);
setStyle("-fx-background-color: #ffc0cb;");
}
...
}
这篇关于javafx datepicker如何自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!