David RJ
David RJ

Reputation: 115

Disable button depending on another node children amount in JavaFX

I need to disable button depending on some element children amount.

I have tried something like this, which is not right:

HBox userDataHBox = new HBox(new Label("1"), new Label("2"), new Label("3"));
Button btn = new Button();
btn.disableProperty().bind(
  Bindings.notEqual(userDataHBox.getChildren().size(), 3)
);

Upvotes: 0

Views: 53

Answers (2)

fabian
fabian

Reputation: 82461

userDataHBox.getChildren().size()

just yields the current size of the list. Nothing to observe there. You could use Bindings.size to get a IntegerBinding for the size that can be used:

btn.disableProperty().bind(
    Bindings.size(userDataHBox.getChildren()).isNotEqualTo(3));

Upvotes: 6

micpog90
micpog90

Reputation: 106

Here you go:

btn.disableProperty().bind(
  Bindings.createBooleanBinding(()-> userDataHBox.getChildren().size() != 3, userDataHBox.getChildren())
);

Upvotes: 4

Related Questions