如何通过比较 2 个 DatePickers 获得 BooleanBinding?
How to get BooleanBinding from comparing 2 DatePickers?
我在 GUI 上有两个 DatePicker
和一个 Button
。每当第一个 datePicker 的日期在第二个 datePicker 的日期之前 NOT 时,我需要禁用该按钮。即 before
在以下代码段中是 false
:
LocalDate date1 = dpFromDate.getValue();
LocalDate date2 = dpToDate.getValue();
boolean before = date1.isBefore(date2);
button.setDisable(!before);
使用绑定 APIs.
BooleanBinding bb = ???;
button.disableProperty().bind(bb);
这是我的工作解决方案,但我相信有更好的 API 来处理这种情况:
BooleanBinding bb = Bindings.selectInteger(dpFromDate.valueProperty(), "year")
.greaterThanOrEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "year"));
bb = bb.or(
Bindings.selectInteger(dpFromDate.valueProperty(), "year")
.isEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "year"))
.and(Bindings.selectInteger(dpFromDate.valueProperty(), "monthValue")
.greaterThanOrEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "monthValue")))
);
bb = bb.or(
Bindings.selectInteger(dpFromDate.valueProperty(), "year")
.isEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "year"))
.and(Bindings.selectInteger(dpFromDate.valueProperty(), "monthValue")
.isEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "monthValue")))
.and(Bindings.selectInteger(dpFromDate.valueProperty(), "dayOfMonth")
.greaterThanOrEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "dayOfMonth")))
);
只需根据比较两个日期的 DatePicker
值创建一个 BooleanBinding
。这样您就不必自己编写功能,更重要的是 - 您不需要创建如此复杂的绑定:
BooleanBinding bb = Bindings.createBooleanBinding(() -> {
LocalDate from = dpFromDate.getValue();
LocalDate to = dpToDate.getValue();
// disable, if one selection is missing or from is not smaller than to
return (from == null || to == null || (from.compareTo(to) >= 0));
}, dpFromDate.valueProperty(), dpToDate.valueProperty());
button.disableProperty().bind(bb);
我在 GUI 上有两个 DatePicker
和一个 Button
。每当第一个 datePicker 的日期在第二个 datePicker 的日期之前 NOT 时,我需要禁用该按钮。即 before
在以下代码段中是 false
:
LocalDate date1 = dpFromDate.getValue();
LocalDate date2 = dpToDate.getValue();
boolean before = date1.isBefore(date2);
button.setDisable(!before);
使用绑定 APIs.
BooleanBinding bb = ???;
button.disableProperty().bind(bb);
这是我的工作解决方案,但我相信有更好的 API 来处理这种情况:
BooleanBinding bb = Bindings.selectInteger(dpFromDate.valueProperty(), "year")
.greaterThanOrEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "year"));
bb = bb.or(
Bindings.selectInteger(dpFromDate.valueProperty(), "year")
.isEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "year"))
.and(Bindings.selectInteger(dpFromDate.valueProperty(), "monthValue")
.greaterThanOrEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "monthValue")))
);
bb = bb.or(
Bindings.selectInteger(dpFromDate.valueProperty(), "year")
.isEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "year"))
.and(Bindings.selectInteger(dpFromDate.valueProperty(), "monthValue")
.isEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "monthValue")))
.and(Bindings.selectInteger(dpFromDate.valueProperty(), "dayOfMonth")
.greaterThanOrEqualTo(Bindings.selectInteger(dpToDate.valueProperty(), "dayOfMonth")))
);
只需根据比较两个日期的 DatePicker
值创建一个 BooleanBinding
。这样您就不必自己编写功能,更重要的是 - 您不需要创建如此复杂的绑定:
BooleanBinding bb = Bindings.createBooleanBinding(() -> {
LocalDate from = dpFromDate.getValue();
LocalDate to = dpToDate.getValue();
// disable, if one selection is missing or from is not smaller than to
return (from == null || to == null || (from.compareTo(to) >= 0));
}, dpFromDate.valueProperty(), dpToDate.valueProperty());
button.disableProperty().bind(bb);