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);
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s