iglu.jdbc
Class JDBCTableModel

java.lang.Object
  |
  +--javax.swing.table.AbstractTableModel
        |
        +--iglu.jdbc.JDBCTableModel
All Implemented Interfaces:
java.io.Serializable, javax.swing.table.TableModel
Direct Known Subclasses:
SQLTableModel

public class JDBCTableModel
extends javax.swing.table.AbstractTableModel

Implements a Table Model that wraps itself around a JDBC result set we have to keep a buffer, and load it with the values which the table is currently asking for. This meakes it easy to shows SQL queries in a table.

Version:
1.0
Author:
Travis Bauer trbauer@indiana.edu
See Also:
Serialized Form

Field Summary
protected  int[] bm
          a hack to get around the fact that the ListSelectionEvent does not really tell you what was selected.
protected  int bookmark
          bookmarked position.
(package private)  int[] colsToDisplay
          which cols to display.
(package private)  int numRows
          the number of rows in this result set
(package private)  java.sql.ResultSet rs
          where the data is stored
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Constructor Summary
JDBCTableModel()
          Uuuhhhh...
JDBCTableModel(java.sql.ResultSet nrs)
          Create a new TableModel based on the result set
JDBCTableModel(java.sql.ResultSet nrs, int[] ncols)
          Create a new TableModel based on the result set.
 
Method Summary
 int bookmark()
          Get the current book mark
protected  void figureNumRows()
          figure out how many row there are
 java.lang.String getBookmarkString(int whichString)
          return the values from the ResultSet at column "whichString".
 int getColumnCount()
          The number of fields which will show up on the table
 java.lang.String getColumnName(int columnIndex)
          Uses the Field names as the name to show in the column
 int getRowCount()
          Return the number of rows in the model
 java.lang.Object getValueAt(int row, int column)
          Returns the value at row, column
 java.sql.ResultSet rs()
          get the result set which underlies the table model
 javax.swing.event.ListSelectionListener setBookmark()
          a listselectionlistener which sets the result set's current row to the highlighted item.
 void setColsToDisplay(int[] n)
          Change the columns from the result set to display.
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getColumnClass, getListeners, getTableModelListeners, isCellEditable, removeTableModelListener, setValueAt
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rs

java.sql.ResultSet rs
where the data is stored


numRows

int numRows
the number of rows in this result set


colsToDisplay

int[] colsToDisplay
which cols to display. To show columns 1, 3, and 6 in the table, set this to: new int[] {1, 3, 6}; You can still get at the other columns in the result set.


bookmark

protected int bookmark
bookmarked position. Used by the selection listener to keep track of the most recently selected item. Initialized to -1 to indicate no selection. 1-base inde


bm

protected int[] bm
a hack to get around the fact that the ListSelectionEvent does not really tell you what was selected. Uses a 1-based index

Constructor Detail

JDBCTableModel

public JDBCTableModel()
Uuuhhhh... Don't do anything.


JDBCTableModel

public JDBCTableModel(java.sql.ResultSet nrs)
Create a new TableModel based on the result set

Parameters:
nrs -

JDBCTableModel

public JDBCTableModel(java.sql.ResultSet nrs,
                      int[] ncols)
Create a new TableModel based on the result set. Only display the columns specified in ncols[]. It is 1-based, just like ResultSet

Parameters:
nrs -
ncols -
Method Detail

getRowCount

public int getRowCount()
Return the number of rows in the model


getColumnCount

public int getColumnCount()
The number of fields which will show up on the table


getValueAt

public java.lang.Object getValueAt(int row,
                                   int column)
Returns the value at row, column

Parameters:
row -
column -

getColumnName

public java.lang.String getColumnName(int columnIndex)
Uses the Field names as the name to show in the column

Specified by:
getColumnName in interface javax.swing.table.TableModel
Overrides:
getColumnName in class javax.swing.table.AbstractTableModel
Parameters:
columnIndex -

bookmark

public int bookmark()
Get the current book mark


setBookmark

public javax.swing.event.ListSelectionListener setBookmark()
a listselectionlistener which sets the result set's current row to the highlighted item. Make sure you add in this action listener if you want the table model to know what the currently highlighted item is!


figureNumRows

protected void figureNumRows()
figure out how many row there are


getBookmarkString

public java.lang.String getBookmarkString(int whichString)
return the values from the ResultSet at column "whichString". This is quite useful, especially if you only want to show certain columns in the table, but want to keep a larger query im memory. Example: Let's say you have a statement: Select name, phone number from myTable. Then you create the TableModel, only displaying the Name. You can still get the phone number programtically by calling getBookmarkString(2). NOTE: This will only work if you add setBookmark selectionListener


setColsToDisplay

public void setColsToDisplay(int[] n)
Change the columns from the result set to display. Useful when you don't know beforehand how many columns will be in the result set


rs

public java.sql.ResultSet rs()
get the result set which underlies the table model