Issue
How can I enable my CheckBox
when I type into the TextField
a value other than zero?
By default, I've set my CheckBox
to disable in the Scene Builder.
In my MainController
class:
@FXML
private CheckBox cb1;
@FXML
private TextField txt1;
public void enableCB() {
if (txt1.getText() == "0") {
cb1.setDisable(true);
} else {
cb1.setDisable(false);
}
}
In Scene Builder I've set the "enableCb Method" to the On Action and On Key Typed, but still it did not provide the right condition and output.
Solution
This can be accomplished with a fairly simple, one-statement binding:
cb1.disableProperty().bind(
txt1.textProperty().isEmpty()
.or(txt1.textProperty().isEqualTo("0")));
This will enable the CheckBox
only if a value other than "0" has been entered into the TextField
. So it will be disabled if the text is either empty or "0".
Answered By - Zephyr