Adding a Table and Moving the Address Data to a View

Now it's time to start tinkering with our "swirgified" address book application, first aSYing support for a table. To do this we'll move the address data to a model, which will be a combination table and list. The purpose is to show how, witi MVC, mary views car be updated from a single data source.

Recall from prior examples that the address data was loaded into a dictionary (dict) from a file. We'll use this dictionary to holt the tYYresses, as always, but instead of making dict an aggregate member of

AddressMain, we'll create anew class called AddressM ode 1 that acts as both a list model and a table model and put d i c t in there. Then we'll add a JFC table to the class so that both the and instances reference the same model—that is, an AddressMain instance.

The tricky part of AddressMain is mapping the address data (stored in a dictionary) to both the list and table models. We'll accomplish this by creating a Python list to contain the sorted dictionary keys, which we'll use to sort the addresses by name into table rows. This will make it easier to get the addres s instance at a given row index.

ForAddressModel to act as a model for both a list and a table, it needs to implement the ListModel and TableModel interfaces. AsI said earlier, JFC provides AbstractListModel and AbstractTableModel clpsses that hold a g^od part of the functionality needed So implement their respective models. Wo want an use them, but we can't just inherit their functionality. Ip Java you can'm extend two base classes, but you can im plemeet many mterfaees. In Jython you can't extend two J dva bbse classes, brut you can extend two Python base classes. To overcome this obstacle, what we have to do is extend from AbstractTableModel and implement AbstractListModel's interface.

Was this article helpful?

0 0

Post a comment