|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.ibm.db.beans.DBTableModel
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 |
public DBTableModel()
Method Detail |
public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- PropertyChangeListenerremovePropertyChangeListener(java.beans.PropertyChangeListener)
public void addTableModelListener(javax.swing.event.TableModelListener listener)
addTableModelListener
in interface javax.swing.table.TableModel
listener
- tableModleListenerremoveTableModelListener(javax.swing.event.TableModelListener)
public java.lang.Class getColumnClass(int columnIndex)
This is used by the table to set up a default renderer and editor for the column.
getColumnClass
in interface javax.swing.table.TableModel
columnIndex
- the first column is 0, the second is 1, ...
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.public int getColumnCount()
Until the statement has been executed, this returns 0. Once it has been executed, it returns the acatual number of columns.
getColumnCount
in interface javax.swing.table.TableModel
getRowCount()
public java.lang.String getColumnName(int columnIndex)
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.
getColumnName
in interface javax.swing.table.TableModel
columnIndex
- the index of column
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.public int getRowCount()
getRowCount
in interface javax.swing.table.TableModel
getColumnCount()
public com.ibm.db.beans.DBSelect getSelect()
setSelect(com.ibm.db.beans.DBSelect)
public int getSelectedRow()
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.
setSelectedRow(int)
public java.lang.Object getValueAt(int rowIndex, int columnIndex)
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.
getValueAt
in interface javax.swing.table.TableModel
rowIndex
- the row numbercolumnIndex
- the column number
setValueAt(java.lang.Object, int, int)
,
DBSelect.getCacheValueAt(int, int)
public boolean isCellEditable(int rowIndex, int columnIndex)
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.
isCellEditable
in interface javax.swing.table.TableModel
rowIndex
- the row numbercolumnIndex
- the column number
DBSelectMetaData.isWritable(int)
public void removePropertyChangeListener(java.beans.PropertyChangeListener listener)
listener
- PropertyChangeListeneraddPropertyChangeListener(java.beans.PropertyChangeListener)
public void removeTableModelListener(javax.swing.event.TableModelListener listener)
removeTableModelListener
in interface javax.swing.table.TableModel
listener
- tableModleListeneraddTableModelListener(javax.swing.event.TableModelListener)
public void setSelect(com.ibm.db.beans.DBSelect select)
select
- the DBSelect or DBProcedureCall beangetSelect()
public void setSelectedRow(int row) throws DBException, java.sql.SQLException
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.
row
- the number of the selected row
DBException
java.sql.SQLException
setSelectedRow(int)
public void setValueAt(java.lang.Object aValue, int rowIndex, int columnIndex)
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.
setValueAt
in interface javax.swing.table.TableModel
aValue
- the new valuerowIndex
- the row whose value is to be changedcolumnIndex
- the column whose value is to be changed
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.getValueAt(int, int)
,
isCellEditable(int, int)
,
DBSelect.setCacheValueAt(java.lang.Object, int, int)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |