Search code examples
buttonjavafxbooleanbinddisabled-input

javafx - Bind button according to boolean value


Here, I want to disable and enable button according to the value of the boolean.

boolean result=(txtItem.getText().isEmpty() && txtQty.getText().isEmpty());

btnOrder.disableProperty().bind(xxxxx);

what should I enter there??


Solution

  • If I understand what you are asking (in particular, assuming txtItem and txtQty are some kind of TextInputControl), you can do

    btnOrder.disableProperty().bind(Bindings.createBooleanBinding(
        () -> txtItem.getText().isEmpty() && txtQty.getText().isEmpty(),
        txtItem.textProperty(), txtQty.textProperty()));
    

    or

    btnOrder.disableProperty().bind(
        Bindings.length(txtItem.textProperty()).isEqualTo(0)
        .and(Bindings.length(txtQty.textProperty()).isEqualTo(0)));