人物类:
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
都使用相同的ListEventPublisher
and ReadWriteLock
。
问题
考虑到,我应该如何将相同的ListEventPublisher
内容传递给每个实例ReadWriteLock
的 theCollectionList
和 resident EventList
sHouse
- 每次我删除或添加居民到房子时,表格都应该更新吗?
- 类的实例
House
可以在创建自身之前和之后创建,CollectionList
并且它们都必须是它的有效条目?
请注意,第二个标准使得仅从the获取Publisher
and并将它们传递给 new s 的构造函数是不可能的。(因为房屋可能在列表本身之前创建)Lock
CollectionList
House
Publisher
除了共享and之外,还有其他解决方法Lock
吗?
相关问题: How to deal with GlazedLists's PluggableList requirements for shared publisher and lock