Brought to you by EarthWeb
ITKnowledge Logo Login Graphic IBM DB2 Universal Database.
IBM DB2 Universal Database.
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


To aid developers creating classes that implement the TableModel interface, the javax.swing.table package contains the AbstractTableModel class. This abstract class manages TableModelListener instances and contains convenience methods for firing TableModelEvents. Classes that extend the AbstractTableModel class must provide implementations for the getRowCount, getColumnCount, and getValueAt methods. This provides the view with a read-only view of the data in the model. The AbstractTableModel class provides default implementation for the isCellEditable and setValueAt methods. The isCellEditable method returns false for all row and column values passed to it. This creates a read-only table allowing the setValueAt method to be empty. The getColumnClass method simply returns the Object class. The getColumnName method labels columns similar to a spreadsheet. Column names are A, B, and so on.

To provide an editable TableModel implementation, the isCellEditable and setValueAt methods must also be overridden. To allow different editors and renderers for different classes of data, the getColumnClass method must be overridden as well. At this point, the extension to AbstractTableModel has nearly implemented the TableModel interface, so it may be asked, “Why extend AbstractTableModel at all?” The extension inherits the TableModelListener management functions, but more significantly, it inherits the fire methods provided by the AbstractTableModel class. These methods allow the model to easily fire TableModelEvents to listeners. The fire methods are shown in Table 12.3.

Table 12.3 AbstractTableModel Classes Fire Methods

Method Name Purpose of Event

FireTableCellUpdated (int row, int column) Notify all listeners that the value of the cell at (row, column) has been updated.
fireTableChanged (TableModel Event e) Forward the given notification event to all TableModel Listeners that registered themselves as listeners for this table model.
fireTableDataChanged Notify all listeners that all cell values in the table’s rows may have changed.
fireTableRowsDeleted (int firstRow, int lastRow) Notify all listeners that rows in the (inclusive) range [firstRow, lastRow] have been deleted.
fireTableRowsInserted(int firstRow, int lastRow) Notify all listeners that rows in the (inclusive) range [firstRow, lastRow] have been inserted.
fireTableRowsUpdated(int firstRow, int lastRow) Notify all listeners that rows in the (inclusive) range [firstRow, lastRow] have been updated.
fireTableStructureChanged() Notify all listeners that the table’s structure has changed.

The TableModelListener interface, shown in Listing 12.3, must be implemented to receive events from the table model. As can be seen from the listing, this is a simple interface. It defines one method, tableChanged. This is called when the table model being listened to changes. The TableModelEvent instance passed as the parameter to this method contains the information as to what type of change occurred. The TableModelEvent class is also shown in Listing 12.3. This class defines constants that define the type of change that occur in the model. Possible types for the event are INSERT, DELETE, and UPDATE. After the type of event has been determined by using the getType method, the range of effected rows can be obtained with the getFirstRow and getLastRow methods. The affected column can be obtained from the getColumn method. The constant ALL_COLUMNS is defined to signify that all columns in the table are involved with the event.

The TableModelEvent also contains special case conditions. If an ALL_COLUMNS UPDATE event is received with a last row of Integer.MAX_VALUE, this means that the entire table’s row data has changed and the view should reread this information from the model. If an ALL_COLUMNS UPDATE event is received for the HEADER_ROW, the structure of the table has changed. The view must reinitialize its columns and refresh the view. These magic event types add a level of complexity to the TableModelEvent that in this author’s opinion was unnecessary. Adding a couple of additional event types, or extending the TableModelListener interface for these special cases, would have made the event flow between the TableModel and its listeners much more clear and alleviated a significant amount of special case code in the listener.


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.