TOC PREV NEXT INDEX



Creating a TableModel


So far, we've connected to a database, we've set up code to send an SQL query to the database, and we've stored the results. We now want to be able to display the data. To do this we will create a TableModel.

  1. From the Part List, select 'table' to view the properties of our JTable.
  2. Choose the TableModel tab. This code area contains the methods which describe the data we wish to place in the table.
  3. Choose 'getColumnCount' from the method choice field (above the code area).
  4. Enter the following code:
try {
return meta.getColumnCount();
} catch ( Exception e ) { return 0; }
  1. Choose 'getColumnName' from the method choice field.
  2. Enter the following code:
try {
return meta.getColumnName(columnIndex+1);
} catch ( Exception e ) { return "Error"; }
  1. Choose 'getRowCount' from the method choice field.
  2. Enter the following code:
try {
return data.size()/meta.getColumnCount();
} catch ( Exception e ) { return 0; }
  1. Choose 'getValueAt' from the method choice field.
  2. Enter the following code:
try {
return data.elementAt(
meta.getColumnCount()*rowIndex + columnIndex);
} catch ( Exception e ) { return "Error"; };

The TableModel code is executed on the fly in the same manner as the event code which you are already familiar with. As you enter each of the above pieces of code, the table will show the results.


Data Representations, Inc.
http://www.datarepresentations.com
support@datarepresentations.com
sales@datarepresentations.com
TOC PREV NEXT INDEX