13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

16.7 Getting Down to Cases: Designing a GUI for BudgetPro363number of subaccounts defined for the account. Since we’re defining ourAbstractTableModel as an inner class, we have access to the data in the outer(BudgetPro) class. We use the instance variable current, which refers towhichever account we’re currently working with. A quick check of the Accountclass shows that an Account object can return the number of subaccounts(or “children”) via its size() method. So for our getRowCount() method wereturn current.size()—provided that current is not null. If it is null,we return 1 rather than 0, so that the table itself shows up and the headingsappear. (But it also means that getValueAt() has to deal with requests fordata from the first row when data may not exist.)The core of what makes our data appear is the getValueAt() method,lines 161–185. Since each row represents a subaccount of the current account,we’ll just iterate through current’s list of subaccounts until we reach therow-th subaccount; for example, to get the third row we iterate over this list ofsubaccounts until we get to the third <strong>on</strong>e returned by the iterator’s next()method. This is a bit “brute force,” to keep marching over the list of accounts,but for our small data size it’s not bad. (Another approach would be to changethe Account class to provide a method to return the n-th subaccount. Then itcan use its internal knowledge of the way it stores subaccounts to provide amore efficient access. Alternately, our extended AbstractTableModel coulditerate over the list <strong>on</strong>ce and store the subaccounts in an array, for quicker accesslater; the trick here is that the array needs to be refreshed every time the accountchanges—so we took the simple approach.)Once we have a row selected, we use the switch/case c<strong>on</strong>struct to choosethe correct data for the requested column. (See the listing in Example 16.2,lines 173–183.)The return value for getValueAt() is an Object. Here’s <strong>on</strong>e situati<strong>on</strong>where that is very useful. Refer to the definiti<strong>on</strong> of the Account object andyou’ll see that getName() returns a String, but getOwner() returns a Userand getTotal() returns an SAM<strong>on</strong>ey object. Since retval is the most generictype, Object, it can handle all three results.But how does JTable deal with these odd types? How can it display anSAM<strong>on</strong>ey object when it doesn’t know what <strong>on</strong>e is? There is both a simple anda complicated answer to that questi<strong>on</strong>; we’ll try to give you both.16.7.2.9 RenderersThe simple answer is that JTable, to display the data returned bygetValueAt(), will call the toString() method <strong>on</strong> the object. As l<strong>on</strong>g as we

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!