【发布时间】:2018-10-23 12:40:58
【问题描述】:
我正在尝试创建与Button#disableProperty() 绑定的BooleanBinding。我的目的是在从动态创建的面板列表更改TextField 时启用/禁用“确定”按钮。
这是面板列表(propertiesList)的初始化
<DialogPane prefWidth="900" prefHeight="600" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1">
<fx:define>
<FXCollections fx:id="propertiesList" fx:factory="observableArrayList">
<DatabasePane name="База данни"/>
<DatabasePane name="База данни 2"/>
</FXCollections>
</fx:define>
....
<buttonTypes>
<ButtonType fx:id="okButtonType" buttonData="OK_DONE" text="Готово" />
<ButtonType buttonData="CANCEL_CLOSE" text="Затвори" />
</buttonTypes>
</DialogPane>
每个DatabasePane 都包含名为change 的BooleanProperty 以及相应的getter 和setter。
在控制器中,我从propertiesList 中添加的面板属性创建了一个BooleanProperty 集合
List<BooleanProperty> list = propertiesList.stream()
.map(pane -> pane.changeProperty())
.collect(Collectors.toList());
BooleanBinding change = Bindings.createBooleanBinding(() -> true, (BooleanProperty[]) list.toArray());
Button button = (Button) dialogPane.lookupButton(okButtonType);
button.disableProperty().bind(change.not());
java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljavafx.beans.property.BooleanProperty; ([Ljava.lang.Object; is in module java.base of loader 'bootstrap'; [Ljavafx.beans.property.BooleanProperty; is in module javafx.base of loader 'app')
如果我使用ObservableList,则没有错误,但只会对列表本身进行更改
ObservableList<BooleanProperty> list = propertiesList.stream()
.map(pane -> pane.changeProperty())
.collect(Collectors.toCollection(() -> FXCollections.observableArrayList()));
BooleanBinding change = Bindings.createBooleanBinding(() -> true, list);
Button button = (Button) dialogPane.lookupButton(okButtonType);
button.disableProperty().bind(change.not());
【问题讨论】:
标签: javafx binding observable