JavaFX - 将属性绑定到可观察集合中每个元素的属性 [英] JavaFX - bind property to properties of every element in observable Collection

查看:25
本文介绍了JavaFX - 将属性绑定到可观察集合中每个元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在将 BooleanProperty 绑定到 ObservableList 中每个元素的连接的方法?

Does exist any method which bind BooleanProperty to conjunction of every element in ObservableList?

ObservableList<BooleanProperty> list;
list = FXCollections.observableList(new ArrayList<BooleanProperty>));
BooleanProperty emptyProperty = new SimpleBooleanProperty();
emptyProperty.bind(Bindings.conunction(list));`

有没有这样的方法:

static BooleanBinding conjunction(ObservableList<BooleanProperty> op)

推荐答案

JavaFX 中没有定义conjunction api2.2平台.

There is no conjunction api defined in the JavaFX 2.2 platform.

您可以通过子类化 布尔绑定.

You could create a ConjunctionBooleanBinding (aka AllTrueBinding) by subclassing BooleanBinding.

在新类的构造函数中接受 ObservableList,并使用低级绑定 api 在被覆盖的 computeValue 方法来设置绑定值基于逻辑和所有布尔值清单.

Accept the ObservableList in the constructor of your new class, and use the low level binding api in an overridden computeValue method to set the binding value based upon logically anding together all of the boolean values in the list.

这是一个示例实现.样本可以进一步优化性能并利用WeakReferences,因此不需要手动处理.

Here is a sample implementation. The sample could be further performance optimized and make use of WeakReferences, so it does not require manual disposition.

import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.collections.*;

public class AllTrueBinding extends BooleanBinding {
  private final ObservableList<BooleanProperty> boundList;
  private final ListChangeListener<BooleanProperty> BOUND_LIST_CHANGE_LISTENER =
    new ListChangeListener<BooleanProperty>() {
      @Override public void onChanged(
             ListChangeListener.Change<? extends BooleanProperty> change
          ) {
        refreshBinding();
      }
    };
  private BooleanProperty[] observedProperties = {};

  AllTrueBinding(ObservableList<BooleanProperty> booleanList) {
    booleanList.addListener(BOUND_LIST_CHANGE_LISTENER);
    boundList = booleanList;
    refreshBinding();
  }

  @Override protected boolean computeValue() {
    for (BooleanProperty bp: observedProperties) {
      if (!bp.get()) {
        return false;
      }
    }

    return true;
  }

  @Override public void dispose() {
    boundList.removeListener(BOUND_LIST_CHANGE_LISTENER);
    super.dispose();
  }

  private void refreshBinding() {
    super.unbind(observedProperties);
    observedProperties = boundList.toArray(new BooleanProperty[0]);
    super.bind(observedProperties);
    this.invalidate();
  }
}

这里有一个测试工具来演示它是如何工作的:

And here is a test harness to demonstrate how it works:

import java.util.*;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class ListBindingTest {
  final BooleanProperty a = new SimpleBooleanProperty(true);
  final BooleanProperty b = new SimpleBooleanProperty(true);
  final BooleanProperty c = new SimpleBooleanProperty(true);
  final BooleanProperty d = new SimpleBooleanProperty(true);
  final ObservableList<BooleanProperty> booleanList =
    FXCollections.observableArrayList(a, b, c, d);

  public static void main(String[] args) {
    new ListBindingTest().test();
  }

  private void test() {
    AllTrueBinding at = new AllTrueBinding(booleanList);

    System.out.println(at.get() + forArrayString(booleanList));

    b.set(false);
    System.out.println(at.get() + forArrayString(booleanList));

    b.set(true);
    System.out.println(at.get() + forArrayString(booleanList));

    booleanList.add(new SimpleBooleanProperty(false));
    System.out.println(at.get() + forArrayString(booleanList));

    booleanList.remove(3, 5);
    System.out.println(at.get() + forArrayString(booleanList));

    at.dispose();
  }  

  private String forArrayString(List list) {
    return " for " + Arrays.toString(list.toArray());
  }
}

这篇关于JavaFX - 将属性绑定到可观察集合中每个元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆