gpt4 book ai didi

list - JavaFX - 将属性绑定(bind)到可观察集合中每个元素的属性

转载 作者:行者123 更新时间:2023-12-02 02:49:13 24 4
gpt4 key购买 nike

是否存在将 BooleanProperty 绑定(bind)到 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)

最佳答案

没有conjunction JavaFX 2.2 平台中定义的 api。

您可以通过子类化 BooleanBinding 创建一个 ConjunctionBooleanBinding (又名 AllTrueBinding) 。

接受ObservableList在新类的构造函数中,并使用 low level binding api在一个被覆盖的computeValue方法根据逻辑上将列表中的所有 bool 值组合在一起来设置绑定(bind)值。

这是一个示例实现。该示例可以进一步进行性能优化并利用WeakReferences,因此不需要手动配置。

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();
}
}

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

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());
}
}

关于list - JavaFX - 将属性绑定(bind)到可观察集合中每个元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13764401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com