com.ibm.db.beans
Class DBTableModel

java.lang.Object
  |
  +--com.ibm.db.beans.DBTableModel
All Implemented Interfaces:
javax.swing.table.TableModel

public class DBTableModel
extends java.lang.Object
implements javax.swing.table.TableModel

This class is an adapter that can serve as the model for a JTable and enable it to display the data from a DBSelect bean. To use it, set the select property of a DBTableModel bean to a DBSelect or DBProcedureCall bean, and set the model property of a JTable to the DBTableModel bean.

This bean fires appropriate events to keep the JTable updated as column values change in the DBSelect, and as rows are inserted and deleted. In addition, it fires a PropertyChangeEvent for its selectedRow property whenever the current row of the DBSelect bean changes.

Note that while rows and columns in the DBSelect use 1-based indexing, this is translated by the DBTableModel to the 0-based indexing required for the JTable.

The adapter functions so that the JTable displays the rows in the cache of the DBSelect bean. If your DBSelect bean limits cache size so that not all rows of the resultSet are in the cache at once, the JTable will also not be displaying all of the rows at once. In this case, you will probably want to provide another control, such as a JButton, to cause the next set of rows to be fetched into the cache. When this happens, the JTable will automatically be updated to display the rows newly fetched into the cache.

To keep the current row of the DBSelect bean synchronized with the selected row of the JTable, you must listen for events fired when the selectedRow of either the JTable or the DBTableModel changes, and propagate the change to the other bean. In one direction, listen for the PropertyChangeEvent fired when the selectedRow of the DBTableModel bean changes, and in response, execute the JTable.setRowSelectionInterval method, passing the selectedRow of the DBTableModel as the start and end parameters. In the other direction, listen for some event, such as MouseClicked, that will be fired whenever the user changes the selectedRow of the JTable, and in response, set the selectedRow of the DBTableModel to the selectedRow of the JTable. You need to enable row selection for the JTable, and also set its selection mode to SINGLE_SELECTION.


Constructor Summary
DBTableModel()
          The default constructor.
 
Method Summary
 void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
          Adds a listener to the PropertyChange event set.
 void addTableModelListener(javax.swing.event.TableModelListener listener)
          Adds a listener to the TableModel event set.
 java.lang.Class getColumnClass(int columnIndex)
          Gets Java class of values in this column.
 int getColumnCount()
          Returns the number of columns in the RowSet.
 java.lang.String getColumnName(int columnIndex)
          Returns the String to appear in the header of the JTable for this column.
 int getRowCount()
          Returns the number of rows cached in the RowSet.
 com.ibm.db.beans.DBSelect getSelect()
          Gets a reference to the DBSelect or DBProcedureCall bean whose data this bean provides access to.
 int getSelectedRow()
          Gets the number of the selected row.
 java.lang.Object getValueAt(int rowIndex, int columnIndex)
          Returns the value at rowIndex and columnIndex in the cache of the associated DBSelect.
 boolean isCellEditable(int rowIndex, int columnIndex)
          Returns true if the cell at rowIndex and columnIndex is editable.
 void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
          Removes a listener to the PropertyChange event set.
 void removeTableModelListener(javax.swing.event.TableModelListener listener)
          Removes a listener to the TableModel event set.
 void setSelect(com.ibm.db.beans.DBSelect select)
          Sets the reference to the DBSelect or DBProcedureCall bean whose data this bean will provide access to.
 void setSelectedRow(int row)
          Sets the selected row.
 void setValueAt(java.lang.Object aValue, int rowIndex, int columnIndex)
          Sets the value at rowIndex and columnIndex in the cache of the associated DBSelect.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DBTableModel

public DBTableModel()
The default constructor.

Method Detail

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Adds a listener to the PropertyChange event set.

Parameters:
listener - PropertyChangeListener
See Also:
removePropertyChangeListener(java.beans.PropertyChangeListener)

addTableModelListener

public void addTableModelListener(javax.swing.event.TableModelListener listener)
Adds a listener to the TableModel event set.

Specified by:
addTableModelListener in interface javax.swing.table.TableModel
Parameters:
listener - tableModleListener
See Also:
removeTableModelListener(javax.swing.event.TableModelListener)

getColumnClass

public java.lang.Class getColumnClass(int columnIndex)
Gets Java class of values in this column.

This is used by the table to set up a default renderer and editor for the column.

Specified by:
getColumnClass in interface javax.swing.table.TableModel
Parameters:
columnIndex - the first column is 0, the second is 1, ...
Returns:
the Java class of values in this column
Throws:
DBRuntimeException - - if a DBException or SQLException is raised. A runtime exception is thrown since the method must conform to the TableModel interface, which declares no exceptions. The exception can occur if there is no column at the specified position.

getColumnCount

public int getColumnCount()
Returns the number of columns in the RowSet. A JTable uses this method to determine how many columns it should create and display.

Until the statement has been executed, this returns 0. Once it has been executed, it returns the acatual number of columns.

Specified by:
getColumnCount in interface javax.swing.table.TableModel
Returns:
the number of columns
See Also:
getRowCount()

getColumnName

public java.lang.String getColumnName(int columnIndex)
Returns the String to appear in the header of the JTable for this column. The index of the first column is 0.

The name returned is actually the column label from the DBStatementMetaData of the associated DBSelect bean, rather than the actual name of the column in the database.

Specified by:
getColumnName in interface javax.swing.table.TableModel
Parameters:
columnIndex - the index of column
Returns:
the name of the column
Throws:
DBRuntimeException - - if a DBException or SQLException is raised. A runtime exception is thrown since the method must conform to the TableModel interface, which declares no exceptions. The exception can occur if there is no column at the specified position.

getRowCount

public int getRowCount()
Returns the number of rows cached in the RowSet. A JTable uses this method to determine how many rows it should create and display.

Specified by:
getRowCount in interface javax.swing.table.TableModel
Returns:
the number or rows
See Also:
getColumnCount()

getSelect

public com.ibm.db.beans.DBSelect getSelect()
Gets a reference to the DBSelect or DBProcedureCall bean whose data this bean provides access to.

Returns:
the DBSelect or DBProcedureCall bean
See Also:
setSelect(com.ibm.db.beans.DBSelect)

getSelectedRow

public int getSelectedRow()
Gets the number of the selected row. The number of the first row is 0.

Note that while rows and columns in the associated DBSelect use 1-based indexing, this is translated by the DBTableModel to the 0-based indexing required for the JTable.

Because this bean displays the rows in the cache of the associated DBSelect bean, the number returned is actually the rowInCache property of the DBSelect bean. This may be different from its row property if you have limited cache size so that not all rows of the resultSet are in the cache at once.

The only condition in which no row is selected is when the cache of the DBSelect bean is empty. In this case, the method returns the value -1.

Returns:
the number of the selected row; -1 if no row is selected
See Also:
setSelectedRow(int)

getValueAt

public java.lang.Object getValueAt(int rowIndex,
                                   int columnIndex)
Returns the value at rowIndex and columnIndex in the cache of the associated DBSelect. The index of the first row or column in the cache is 0.

Note that while rows and columns in the associated DBSelect use 1-based indexing, this is translated by the DBTableModel to the 0-based indexing required for the JTable.

Specified by:
getValueAt in interface javax.swing.table.TableModel
Parameters:
rowIndex - the row number
columnIndex - the column number
Returns:
the value Object at the specified row and column
See Also:
setValueAt(java.lang.Object, int, int), DBSelect.getCacheValueAt(int, int)

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Returns true if the cell at rowIndex and columnIndex is editable. Otherwise, setValueAt() on the cell will not change the value of that cell. The index of the first row or column is 0.

Note that while rows and columns in the associated DBSelect use 1-based indexing, this is translated by the DBTableModel to the 0-based indexing required for the JTable.

In practice, for a DBSelect, whether a cell is editable can be specified at the granularity of a column. This method uses the method DBStatementMetaData.isWritable to determine whether a cell is editable, and it will return the same value for each cell in a given column. To prevent all columns from being editable, use the DBSelect.setReadOnly method. To prevent a specific column from being editable, use the DBSelectMetaData.setUpdateEnabled method.

Specified by:
isCellEditable in interface javax.swing.table.TableModel
Parameters:
rowIndex - the row number
columnIndex - the column number
Returns:
true if the cell is editable.
See Also:
DBSelectMetaData.isWritable(int)

removePropertyChangeListener

public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
Removes a listener to the PropertyChange event set.

Parameters:
listener - PropertyChangeListener
See Also:
addPropertyChangeListener(java.beans.PropertyChangeListener)

removeTableModelListener

public void removeTableModelListener(javax.swing.event.TableModelListener listener)
Removes a listener to the TableModel event set.

Specified by:
removeTableModelListener in interface javax.swing.table.TableModel
Parameters:
listener - tableModleListener
See Also:
addTableModelListener(javax.swing.event.TableModelListener)

setSelect

public void setSelect(com.ibm.db.beans.DBSelect select)
Sets the reference to the DBSelect or DBProcedureCall bean whose data this bean will provide access to.

Parameters:
select - the DBSelect or DBProcedureCall bean
See Also:
getSelect()

setSelectedRow

public void setSelectedRow(int row)
                    throws DBException,
                           java.sql.SQLException
Sets the selected row. The number of the first row is 0.

Note that while rows and columns in the associated DBSelect use 1-based indexing, this is translated by the DBTableModel to the 0-based indexing required for the JTable.

Because this bean displays the rows in the cache of the associated DBSelect bean, the number specified actually becomes the new value of the rowInCache property of the DBSelect bean. This may be different from its row property if you have limited cache size so that not all rows of the resultSet are in the cache at once.

Parameters:
row - the number of the selected row
DBException
java.sql.SQLException
See Also:
setSelectedRow(int)

setValueAt

public void setValueAt(java.lang.Object aValue,
                       int rowIndex,
                       int columnIndex)
Sets the value at rowIndex and columnIndex in the cache of the associated DBSelect. The index of the first row or column in the cache is 0.

Note that while rows and columns in the associated DBSelect use 1-based indexing, this is translated by the DBTableModel to the 0-based indexing required for the JTable.

Specified by:
setValueAt in interface javax.swing.table.TableModel
Parameters:
aValue - the new value
rowIndex - the row whose value is to be changed
columnIndex - the column whose value is to be changed
Throws:
DBRuntimeException - - if a DBException or SQLException is raised. A runtime exception is thrown since the method must conform to the TableModel interface, which declares no exceptions. Possible exception conditions include specfying a row that is not in the cache, in addtion to any of the error conditions for the setCacheValueAt method on DBSelect.
See Also:
getValueAt(int, int), isCellEditable(int, int), DBSelect.setCacheValueAt(java.lang.Object, int, int)