Search code examples
javajavafxjavafx-bindings

How to bind a value to an ObservableList's size?


I want to bind a value to the ObservableList's size to know its size and also to know if it has multiple values

private ObservableList<String> strings = FXCollections.observableArrayList();

Solution

  • They can be bound with the Bindings class:

    ObservableList<String> strings = FXCollections.observableArrayList();
    IntegerBinding sizeProperty = Bindings.size(strings);
    BooleanBinding multipleElemsProperty = new BooleanBinding() {
        @Override protected boolean computeValue() {
            return strings.size() > 1;
        }
    };