Java 将按钮绑定到多个属性不起作用
我的按钮被禁用,但一旦我填写我的用户名、密码和组合框,我希望按钮被启用。所以我使用了绑定,但是当我将它与我的组合框一起使用时,我得到了一个绑定不能应用于给定类型的错误。有没有其他方法可以做到这一点,因为我想在将来添加日期和微调器Java 将按钮绑定到多个属性不起作用,java,javafx,binding,Java,Javafx,Binding,我的按钮被禁用,但一旦我填写我的用户名、密码和组合框,我希望按钮被启用。所以我使用了绑定,但是当我将它与我的组合框一起使用时,我得到了一个绑定不能应用于给定类型的错误。有没有其他方法可以做到这一点,因为我想在将来添加日期和微调器 button.disableProperty().bind( Bindings.or( username.textProperty().isEmpty(), password.textProperty()
button.disableProperty().bind(
Bindings.or(
username.textProperty().isEmpty(),
password.textProperty().isEmpty(),
comboBox.valueProperty().isNull()
)
);
绑定。或
只接受2个参数,而不是3个。您需要应用或两次:
button.disableProperty().bind(
username.textProperty().isEmpty().or(
password.textProperty().isEmpty().or(
comboBox.valueProperty().isNull()))
);
或者,您可以使用createBooleanBinding
,这也将允许更复杂的可读表达式:
button.disableProperty().bind(Bindings.createBooleanBinding(
() -> username.getText().isEmpty() || password.getText().isEmpty() || (comboBox.getValue() == null),
username.textProperty(), password.textProperty(), comboBox.valueProperty()
));
绑定。或
只接受2个参数,而不是3个。您需要应用或两次:
button.disableProperty().bind(
username.textProperty().isEmpty().or(
password.textProperty().isEmpty().or(
comboBox.valueProperty().isNull()))
);
或者,您可以使用createBooleanBinding
,这也将允许更复杂的可读表达式:
button.disableProperty().bind(Bindings.createBooleanBinding(
() -> username.getText().isEmpty() || password.getText().isEmpty() || (comboBox.getValue() == null),
username.textProperty(), password.textProperty(), comboBox.valueProperty()
));
副本中的Look at@James\u D答案可能重复。此部分可能稍有不同comboBox.valueProperty().isNull()
。不确定。@Sedrick它不一样,因为它包括valueProperty和textProperty,如James_D的回答所示。我想它不完全一样。想法是一样的。副本中的Look at@James\u D答案可能是重复的。这部分可能略有不同comboBox.valueProperty().isNull()
。不确定。@Sedrick它不一样,因为它包括valueProperty和textProperty,如James_D的回答所示。我想它不完全一样。想法是一样的谢谢Fabian谢谢Fabian。