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.

368Chapter 16Getting in the Swing of Things: Designing a GUI for BudgetProAs l<strong>on</strong>g as this returned Account is not null (line 66), we can make it thecurrent account (line 67). At that point the display needs to be updated, so we:1) call our own setStatus() method, to update the upper porti<strong>on</strong> of ourGUI, and 2) tell the table that its data has changed (line 71).16.7.2.11 Ready, aim, fire!A word about the fire...() methods. They are not part of the TableModelinterface definiti<strong>on</strong>. Rather, they are part of the AbstractTableModel class.When a Java class is declared abstract it means that some methods need to beimplemented by those classes that use (extend) this class. An abstract class canstill have lots of intact, completely implemented methods, and that is the casewith AbstractTableModel.The TableModel interface defines methods for adding and removing listeners.Any implementati<strong>on</strong> of the TableModel interface needs to supportthese, and to notify any listeners when a change occurs. Such listeners will receivea call to their tableChanged() method when such a change occurs. Butit doesn’t tell us how such notificati<strong>on</strong> is triggered. Moreover, the change event,when received by the listener, needs to define the extent of the change—just asingle cell? a whole row? a column? all columns? and so <strong>on</strong>.The AbstractTableModel provides some methods for us to call when achange in the data has occurred, methods that will then notify all the registeredlisteners (Table 16.1). Moreover, it has different methods depending <strong>on</strong> theextent of the change, so that the TableModelEvent, sent to allTableModelListeners, can be c<strong>on</strong>structed with the appropriate definiti<strong>on</strong> ofwhat has changed.We used (line 71) the fireTableDataChanged() since the c<strong>on</strong>tent ofthe table will change with a change of accounts, but the structure remains thesame. It is also a handy all-purpose method for you to use if you’d rather notadd the complexity of determining which rows have changed to your code.Finally, remember that any<strong>on</strong>e who uses (extends) AbstractTableModel,including the DefaultTableModel class, gets these methods for their use.There are several other interacti<strong>on</strong>s that are supported by JTables, <strong>on</strong>esthat d<strong>on</strong>’t require you to do anything to provide them to your applicati<strong>on</strong>’send user. When running the BudgetPro GUI, did you try to drag the columnheadings? You can also rearrange and resize columns. This is the default behaviorfor JTables. You can turn it off, however, if you want your columns tobe fixed:

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

Saved successfully!

Ooh no, something went wrong!