0

人物类:

public class Person {

    private final String name;
    private final int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

房屋类:

public class House {

    private final EventList<Person> residents = new BasicEventList<>();

    public void addResident(Person resident) {
        residents.add(resident);
    }

    public EventList<Person> getResidents() {
        return residents;
    }
}

初始化代码:

EventList<House> houses = new BasicEventList<>();

CollectionList<House, Person> allResidents = new CollectionList<>(houses, new CollectionList.Model<House, Person>() {

    @Override
    public List<Person> getChildren(House parent) {
        return parent.getResidents();
    }
});

jTable.setModel(GlazedListsSwing.eventTableModel(allResidents, new String[]{"name", "age"}, new String[]{"Name", "Age"}, new boolean[]{false, false}));

House firstHouse = new House();
houses.add(firstHouse);
firstHouse.addResident(new Person("John", 18));

House secondHouse = new House();
houses.add(secondHouse);
secondHouse.addResident(new Person("Mary", 44));
secondHouse.addResident(new Person("Alisa", 6));

所以有房屋,其中包含居民名单。该表应显示所有房屋的所有居民。

新居民可以随时添加到房屋中,因此表格应该能够反映变化。这就是为什么EventList将居民存储在一所房子中的明显选择。

但是,GlazedLists 要求 theCollectionList和 residentEventList都使用相同的ListEventPublisherand ReadWriteLock

问题

考虑到,我应该如何将相同的ListEventPublisher内容传递给每个实例ReadWriteLock的 theCollectionList和 resident EventListsHouse

  • 每次我删除或添加居民到房子时,表格都应该更新吗?
  • 类的实例House可以在创建自身之前之后创建,CollectionList并且它们必须是它的有效条目?

请注意,第二个标准使得仅从the获取Publisherand并将它们传递给 new s 的构造函数是不可能的。(因为房屋可能在列表本身之前创建)LockCollectionListHouse

Publisher除了共享and之外,还有其他解决方法Lock吗?


相关问题: How to deal with GlazedLists's PluggableList requirements for shared publisher and lock

4

1 回答 1

0

没有关于它的文档,但这种方法适用于共享ListEventPublisherReadWriteLock列表之间:

public static final ListEventPublisher PUBLISHER = ListEventAssembler.createListEventPublisher();
public static final ReadWriteLock LOCK = LockFactory.DEFAULT.createReadWriteLock();

将这些变量放在某个地方,然后将它们传递给 newEventList的每个构造函数

我通过查看 GlazedLists 的源代码发现了这一点,并看到他们以这种方式创建锁和发布者。不幸的是,我不知道这种方法是否有任何缺点,或者它是否是正确的方法。

于 2016-10-28T17:29:06.077 回答