BooleanBinding 与 selectionModelProperty 问题
BooleanBinding with selectionModelProperty problem
我有一个包含文本字段、组合框和按钮的窗格。我希望该按钮仅在文本字段具有某些值并且组合已选择某些元素时启用,如果没有则禁用。默认情况下,组合没有选择。
我试过这个:
button.disableProperty().bind(Bindings.createBooleanBinding(() ->
(combo.getSelectionModel().getSelectedIndex() == -1) ||
textfield.getText().trim().isEmpty(),
textfield.textProperty(),
combo.selectionModelProperty()
));
但不起作用。如果我删除 combo.selectionModelProperty() 按钮根据文本字段内容启用和禁用正确性,那么 combo.selectionModelProperty() 似乎没有检测到组合中的项目更改。有没有其他方法可以监听物品组合变化?
一个ComboBox
的选择模型本身是可以替换的,但这种情况不常见,需要您自己进行替换。您正在尝试收听 SelectionModel
的 selectedIndex
属性,因此您应该使用
combo.getSelectionModel().selectedIndexProperty()
或更好地使用 ComboBox.value
属性:
button.disableProperty().bind(Bindings.createBooleanBinding(
() -> (combo.getValue() == null) || textfield.getText().trim().isEmpty(),
textfield.textProperty(),
combo.valueProperty()
));
我有一个包含文本字段、组合框和按钮的窗格。我希望该按钮仅在文本字段具有某些值并且组合已选择某些元素时启用,如果没有则禁用。默认情况下,组合没有选择。 我试过这个:
button.disableProperty().bind(Bindings.createBooleanBinding(() ->
(combo.getSelectionModel().getSelectedIndex() == -1) ||
textfield.getText().trim().isEmpty(),
textfield.textProperty(),
combo.selectionModelProperty()
));
但不起作用。如果我删除 combo.selectionModelProperty() 按钮根据文本字段内容启用和禁用正确性,那么 combo.selectionModelProperty() 似乎没有检测到组合中的项目更改。有没有其他方法可以监听物品组合变化?
一个ComboBox
的选择模型本身是可以替换的,但这种情况不常见,需要您自己进行替换。您正在尝试收听 SelectionModel
的 selectedIndex
属性,因此您应该使用
combo.getSelectionModel().selectedIndexProperty()
或更好地使用 ComboBox.value
属性:
button.disableProperty().bind(Bindings.createBooleanBinding(
() -> (combo.getValue() == null) || textfield.getText().trim().isEmpty(),
textfield.textProperty(),
combo.valueProperty()
));