Person class:
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;
}
}
House class:
public class House {
private final EventList<Person> residents = new BasicEventList<>();
public void addResident(Person resident) {
residents.add(resident);
}
public EventList<Person> getResidents() {
return residents;
}
}
init code:
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));
So there are houses, which contain lists of residents. The table should show all residents from all the houses.
New residents can be added to a house at any time, so the table should be able to reflect the changes. This is why EventList
is an obvious choice to store the residents in one house.
However, GlazedLists requires that the CollectionList
and the resident EventList
s all use the same ListEventPublisher
and ReadWriteLock
.
The question
How am I supposed to pass the same ListEventPublisher
and ReadWriteLock
to both the CollectionList
and the resident EventList
s of every House
instance, considering that
- the table should update every time I remove or add a resident to a house?
- instances of
House
class may be created both before and after the creation of theCollectionList
itself and they all must be valid entries to it?
Note that the second criterion makes it impossible to just get the Publisher
and Lock
from the CollectionList
and pass them to the constructors of new House
s. (because the houses may be created before the list itself)
Are there maybe other workarounds than sharing the Publisher
and Lock
?
Related question: How to deal with GlazedLists's PluggableList requirement for shared publisher and lock