1   package groovy.swing;
2   
3   import java.util.logging.Level;
4   import java.util.logging.Logger;
5   
6   import javax.swing.table.AbstractTableModel;
7   
8   /*** 
9    * A sample table model
10   *
11   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
12   * @version $Revision: 1.2 $
13   */
14  public class MyTableModel extends AbstractTableModel {
15  
16      private static final Logger log = Logger.getLogger(MyTableModel.class.getName());
17  
18      public MyTableModel() {
19      }
20  
21      final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
22      final Object[][] data = { { "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {
23              "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)
24              }, {
25              "Kathy", "Walrath", "Chasing toddlers", new Integer(2), new Boolean(false)
26              }, {
27              "Mark", "Andrews", "Speed reading", new Integer(20), new Boolean(true)
28              }, {
29              "Angela", "Lih", "Teaching high school", new Integer(4), new Boolean(false)
30              }
31      };
32  
33      public int getColumnCount() {
34          return columnNames.length;
35      }
36  
37      public int getRowCount() {
38          return data.length;
39      }
40  
41      public String getColumnName(int col) {
42          return columnNames[col];
43      }
44  
45      public Object getValueAt(int row, int col) {
46          return data[row][col];
47      }
48  
49      /*
50       * JTable uses this method to determine the default renderer/
51       * editor for each cell.  If we didn't implement this method,
52       * then the last column would contain text ("true"/"false"),
53       * rather than a check box.
54       */
55      public Class getColumnClass(int c) {
56          return getValueAt(0, c).getClass();
57      }
58  
59      /*
60       * Don't need to implement this method unless your table's
61       * editable.
62       */
63      public boolean isCellEditable(int row, int col) {
64          //Note that the data/cell address is constant,
65          //no matter where the cell appears onscreen.
66          if (col < 2) {
67              return false;
68          }
69          else {
70              return true;
71          }
72      }
73  
74      /*
75       * Don't need to implement this method unless your table's
76       * data can change.
77       */
78      public void setValueAt(Object value, int row, int col) {
79          if (log.isLoggable(Level.FINE)) {
80              log.fine(
81                  "Setting value at " + row + "," + col + " to " + value + " (an instance of " + value.getClass() + ")");
82          }
83  
84          if (data[0][col] instanceof Integer && !(value instanceof Integer)) {
85              //With JFC/Swing 1.1 and JDK 1.2, we need to create    
86              //an Integer from the value; otherwise, the column     
87              //switches to contain Strings.  Starting with v 1.3,   
88              //the table automatically converts value to an Integer,
89              //so you only need the code in the 'else' part of this 
90              //'if' block.                                          
91              //XXX: See TableEditDemo.java for a better solution!!!
92              try {
93                  data[row][col] = new Integer(value.toString());
94                  fireTableCellUpdated(row, col);
95              }
96              catch (NumberFormatException e) {
97                  log.log(Level.SEVERE, "The \"" + getColumnName(col) + "\" column accepts only integer values.");
98              }
99          }
100         else {
101             data[row][col] = value;
102             fireTableCellUpdated(row, col);
103         }
104     }
105 
106 }