Table Model


The JTable component stores data in what is called a table model . You can populate this model by using any of several JTable constructors. Listing 15.1 demonstrated a simple two-parameter construction. What if you wanted to have more control over this data that feeds the JTable? You would use what Sun calls a table model, which implements the model portion of the Model-View-Controller (MVC) pattern.

The table model is a way to abstract the rows and columns of data in the JTable. To do that, you can write your own model that implements the TableModel interface. The more common approach is to create a concrete TableModel subclass of AbstractTableModel . To do so, you need to include only the following three methods in your model class:

 public int getRowCount(); public int getColumnCount(); public Object getValueAt(int row, int column); 

The following code is a simple example of a custom JTable model. The data and column names are already hard-coded, but it demonstrates the bare minimum you are required to code to provide a model for your JTable:

 class MyTableModel extends AbstractTableModel {     final String[] columnNames = {"Gate",                                   "Level",                                   "Aisle",                                   "Row",                                   "Seat"};     final Object[][] data =              {                 {"North", new Integer(100),                  "First", "A", new Integer(1)},                 {"North", new Integer(100),                  "Second", "V", new Integer(24)},                 {"North", new Integer(100),                  "First", "B", new Integer(39)},                 {"South", new Integer(150),                  "Second", "C", new Integer(18)},                 {"South", new Integer(150),                  "Third", "E", new Integer(6)}              };      public int getColumnCount()      {         return columnNames.length;      }      public int getRowCount()      {         return data.length;      }      public Object getValueAt(int row, int col)      {         return data[row][col];      } } 

You can see that it is not difficult to write your own JTable model. For the certification project, you need to provide a way to populate the model with data from a database. The assignment from Sun provides a simple database system, from which you get data to populate the JTable model. Furthermore, through user action, probably on the JTable component, you need to update data in the database.

The trick is to dynamically populate the columnNames and data variables in your model. Listing 15.3 is an edited version of a JTable model used in the book's sample application and represents a good skeleton for your assignment needs.

Listing 15.3 A Sample JTable Model
 import javax.swing.table.AbstractTableModel; import java.io.RandomAccessFile; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; class MyTableModel extends AbstractTableModel {     private Object[][] data;     private String[] columnNames;     //Populate model with data already parsed and placed in 2D array     public MyTableModel (String[][] values, String[] columnNames)     {         this.columnNames = columnNames;         int recordNumber = values.length;         int columnCount = columnNames.length;         data = new Object[recordNumber][columnCount];         for(int rowCount=0; rowCount<recordNumber; rowCount++)         {             for(int colCount=0; colCount<columnCount; colCount++)             {                     setData(values[rowCount], rowCount, colCount);             }//end for         }//end for     }     //You can use this constructor to populate the model with     //data from the database file.     public MyTableModel (String databaseFileName)     {       try       {         //Open databaseFileName with RandomAccessFile object.         //Read column names into columnNames array, then:         File file = new File(databaseFileName);         if (file.exists())         {             RandomAccessFile databaseFile =                      new RandomAccessFile(file, "rw");             //db file probably has header information at top             int columns = databaseFile.readInt();             //more header initiation...              columnNames = new String[columns];             for (int i=0; i<columns; i++)             {                 //fill columnNames[i]             }             this.columnNames = columnNames;             //Read row values into an array of data, then             //add those arrays into a 2D data array:             this.data = data;         }       } catch(FileNotFoundException fnfe)       {            System.out.print(fnfe);       } catch(IOException ex)       {            System.out.print(ex);       }     }     public void setData(String[] values, int row, int column)     {         switch (column)         {                 case (1): //level                 case (4): //seat                         data[row][column] =                              new Integer(values[column].trim());                         break;                 default:                         //is a String already                         data[row][column] = values[column].trim();          }//end switch     }      public int getColumnCount()     {         return columnNames.length;     }     public int getRowCount()     {         return data.length;     }      public String getColumnName(int col)     {         return columnNames[col];     }      public Object getValueAt(int row, int col)     {         return data[row][col];     }      /*      * This method helps renderer for each column.      */     public Class getColumnClass(int c)     {         return getValueAt(0, c).getClass();     } } 


JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net