Teaching and Learning Resources for IB Computer Science

Using JLists with the ListModel interface

Purists may prefer only to allow the View to communicate with the Model via the Controller but in the types of small applications that we do in schools I think it’s perfectly reasonable for the View to have a direct reference to the Model.

// In the Model
private Set people = new HashSet();
public List getPeople() {
return new ArrayList(people);
}
// In the View
listModel.clear();
List people = model.getPeople();
for (Person person : people) {
listModel.addElement(person);
}