Class Hierarchy All Classes All Fields and Methods
Class com.ibm.db.beans.DBTableModel
java.lang.Object com.ibm.db.beans.DBTableModelpublic class DBTableModel
implements TableModel
extends ObjectThis 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, 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 Index Constructor Description DBTableModel() The default constructor.
Method Index Method Description void addPropertyChangeListener(PropertyChangeListener) Adds a listener to the PropertyChange event set. void addTableModelListener(TableModelListener) Adds a listener to the TableModel event set. Class getColumnClass(int) Gets Java class of values in this column. int getColumnCount() Returns the number of columns in the RowSet. String getColumnName(int) 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. 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. Object getValueAt(int, int) Returns the value at rowIndex and columnIndex in the cache of the associated DBSelect. boolean isCellEditable(int, int) Returns true if the cell at rowIndex and columnIndex is editable. void removePropertyChangeListener(PropertyChangeListener) Removes a listener to the PropertyChange event set. void removeTableModelListener(TableModelListener) Removes a listener to the TableModel event set. void setSelect(DBSelect) Sets the reference to the DBSelect or DBProcedureCall bean whose data this bean will provide access to. void setSelectedRow(int) Sets the selected row. void setValueAt(Object, int, int) Sets the value at rowIndex and columnIndex in the cache of the associated DBSelect.
Constructors
DBTableModel
public DBTableModel()The default constructor.
Methods
addPropertyChangeListener
public synchronized void addPropertyChangeListener(PropertyChangeListener listener)Adds a listener to the PropertyChange event set.
- Parameters
- listener - PropertyChangeListener
- See Also
addTableModelListener
public synchronized void addTableModelListener(TableModelListener listener)Adds a listener to the TableModel event set.
- Parameters
- listener - tableModleListener
- See Also
getColumnClass
public 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.
- 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.
- Returns
- the number of columns
- See Also
getColumnName
public 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.
- 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.
- Returns
- the number or rows
- See Also
getSelect
public 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
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
getValueAt
public 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.
- Parameters
- rowIndex - the row number
- columnIndex - the column number
- Returns
- the value Object at the specified row and column
- See Also
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.
- Parameters
- rowIndex - the row number
- columnIndex - the column number
- Returns
- true if the cell is editable.
- See Also
removePropertyChangeListener
public synchronized void removePropertyChangeListener(PropertyChangeListener listener)Removes a listener to the PropertyChange event set.
- Parameters
- listener - PropertyChangeListener
- See Also
removeTableModelListener
public synchronized void removeTableModelListener(TableModelListener listener)Removes a listener to the TableModel event set.
- Parameters
- listener - tableModleListener
- See Also
setSelect
public synchronized void setSelect(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
setSelectedRow
public void setSelectedRow(int row) throws DBException, SQLExceptionSets 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
- See Also
setValueAt
public void setValueAt(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.
- 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
Class Hierarchy All Classes All Fields and Methods