An Easier Way to Create the Address Book

An easier way to crcete the address bookie to use the AbstractListModel class, which implements all of the methods for eegistering event listeners and notifying them of changes. Here's the chaigedJList_AddressModel JListAddrtssMsdtl2.py), which uses AbstractListModel forthe superclass of AddressBook.

class AddressBook(AbstractListModel): def _init_(self):

# Fill in some sample data self.list_items = [Address("Darrell", "555-1212")]

self.list_items.append(Address("Chris", "555-1212"))

# public int getSize()

# Returns the length of the list. def getSize(self):

return len(self.list_items)

# public Object getElementAt (int index)

# Returns the value at the specified index. def getElementAt(self, index) :

return self.list_items[index]

def AddAddress(self, name, number):

self.list_items.append(Address(name, number)) start = len(self.list_items) end = len(self.list_items)

fireIntervalAdded(self, type, start, end)

Notice that the code to implement AddressBook (ListModel) is now significantly shorter. I recommend using the AbstractListModel class instead of the ListMode l interface, which I used simply to explain MVC.

Was this article helpful?

0 0

Post a comment