13

我有一个TableView,我想将 a 的禁用属性与表模型的Button大小绑定。特别是,当 的大小大于 2ObservableList时,我想禁用该按钮。ObservableList

我怎样才能做到这一点?

在我使用的表中未选择任何行时禁用另一个按钮

editRoadButton.disableProperty().bind(roadsTable.getSelectionModel().selectedItemProperty().isNull());

有没有类似的方法?

4

2 回答 2

20

Bindings 类中有用于有用绑定的工厂方法。在你的情况下:

button.disableProperty().bind(Bindings.size(items).greaterThan(2));
于 2014-06-17T09:21:01.707 回答
2

你可以做这样的事情

ListProperty<String> list = new SimpleListProperty<>(FXCollections.<String>emptyObservableList());
Button foo = new Button();

foo.disableProperty().bind(new BooleanBinding() {
    {
        bind(list);
    }

    @Override
    protected boolean computeValue() {
        return list.size() > 2;
    }
});
于 2014-06-16T15:23:21.160 回答