如何将可观察的集合转换为可观察的列表 [英] How to convert an observableset to an observablelist

查看:125
本文介绍了如何将可观察的集合转换为可观察的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将项目设置为tableview,但是当我的模型中有一个observableset时,setitems方法需要一个observablelist.FXCollections实用程序类没有给定可观察集的创建可观察列表的方法.但这导致了类强制转换异常(如预期的那样).

I am trying to set items to a tableview but the setitems method expects an observablelist while I have an observableset in my model.The FXCollections utility class does not have a method for creating an observable list given an observable set.I tried casting but that caused a class cast exception (as expected).

当前我正在使用这种代码

Currently I am using this kind of code

new ObservableListWrapper<E>(new ArrayList<E>(pojo.getObservableSet()));

我有一些问题:

  • 在表中进行编辑是否会按预期更新基础集?
  • 是这样做的正确"方法吗?

因此,简而言之,我需要样式指南或最佳做法,以便在可观察集和可观察列表之间进行转换,因为我希望在构建Java fx GUI时会做很多工作

So in short I need a style guide or best practice for converting between observable set and observable list because I expect to be doing this a lot when building a java fx GUI

推荐答案

是否可以在表中进行编辑以按预期更新基础集?

否,因为您正在复制集合:

No because, you are doing a copy of the set:

new ArrayList<E>(pojo.getObservableSet()

是这样做的正确"方法吗?

我认为正确的方法是不这样做. Set不是List,反之亦然.两者都有特定的矛盾.例如,列表是有序的,并且集合不包含重复的元素.

I think the right way is not doing it. Set are not List and vice versa. Both have specific contraints. For example, the lists are ordered and sets contains no duplicate elements.

此外,也 FXCollections 也不 Bindings 提供此类支持的东西.

Moreover, nor FXCollections nor Bindings provides this kind of stuff.

我希望将集合保留为一组以强制唯一性

我想您可以编写自定义的ObservableList,例如

I guess you could write a custom ObservableList, for example the Parent::children have a similar behavior. It throws an IllegalArgumentException if a duplicate children is added. If you look at the source code, you will see that it is a VetoableListDecorator extension. You could write your own:

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import com.sun.javafx.collections.VetoableListDecorator;

public class CustomObservableList<E> extends VetoableListDecorator<E> {

    public CustomObservableList(ObservableList<E> decorated) {
        super(decorated);
    }

    @Override
    protected void onProposedChange(List<E> toBeAdded, int... indexes) {
        for (E e : toBeAdded) {
            if (contains(e)) {
                throw new IllegalArgumentException("Duplicament element added");
            }
        }
    }
}

class Test {
    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();

        Set<Object> set = new HashSet<Object>();
        set.add(o1);
        CustomObservableList<Object> list = new CustomObservableList<Object>(FXCollections.observableArrayList(set));
        list.add(o2);
        list.add(o1); // throw Exception
    }
}

这篇关于如何将可观察的集合转换为可观察的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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