如何在某些文本字段填满之前禁用按钮?

Thi*_* D. 0 java javafx

使用Java FX 8,我有两个文本字段和一个要验证的按钮.我想要禁用此按钮,直到两个字段都有一个有效值.

做这个的最好方式是什么 ?

谢谢,

Jam*_*s_D 8

使用绑定.

特定

TextField textField1  ;
TextField textField2  ;
Run Code Online (Sandbox Code Playgroud)

你可以做

BooleanBinding textField1Valid = Bindings.createBooleanBinding(() -> {
    // check textField1.getText() and return true/false as appropriate
}, textField1.textProperty());

BooleanBinding textField2Valid = Bindings.createBooleanBinding(() -> {
    // check textField2.getText() and return true/false as appropriate
}, textField2.textProperty());
Run Code Online (Sandbox Code Playgroud)

然后

Button button = new Button("OK");
button.disableProperty().bind(textField1Valid.not().or(textField2Valid.not()));
Run Code Online (Sandbox Code Playgroud)