如何通过比较2个DatePickers得到BooleanBinding?

时间:2016-10-06 06:11:40

标签: java javafx datepicker javafx-8 javafx-bindings

我在GUI上有两个DatePicker和一个Button。每当第一个datePicker在第二个datePicker的日期之前具有 NOT 的日期时,我需要禁用该按钮。即before false在以下代码段中为LocalDate date1 = dpFromDate.getValue(); LocalDate date2 = dpToDate.getValue(); boolean before = date1.isBefore(date2); button.setDisable(!before);

BooleanBinding bb = ???;
button.disableProperty().bind(bb);

使用Bindings 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")))
   );

这是我的工作解决方案,但我相信有更好的API来处理这种情况:

"select * from ["+excel[i]+"]")

1 个答案:

答案 0 :(得分:4)

根据比较两个日期的BooleanBinding值,只需创建DatePicker即可。这样您就不必自己编写功能,甚至更重要 - 您不需要创建这样复杂的绑定:

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);