KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > groovy > swing > MyTableModel


1 package groovy.swing;
2
3 import java.util.logging.Level JavaDoc;
4 import java.util.logging.Logger JavaDoc;
5
6 import javax.swing.table.AbstractTableModel JavaDoc;
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 JavaDoc {
15
16     private static final Logger JavaDoc log = Logger.getLogger(MyTableModel.class.getName());
17
18     public MyTableModel() {
19     }
20
21     final String JavaDoc[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };
22     final Object JavaDoc[][] data = { { "Mary", "Campione", "Snowboarding", new Integer JavaDoc(5), new Boolean JavaDoc(false)}, {
23             "Alison", "Huml", "Rowing", new Integer JavaDoc(3), new Boolean JavaDoc(true)
24             }, {
25             "Kathy", "Walrath", "Chasing toddlers", new Integer JavaDoc(2), new Boolean JavaDoc(false)
26             }, {
27             "Mark", "Andrews", "Speed reading", new Integer JavaDoc(20), new Boolean JavaDoc(true)
28             }, {
29             "Angela", "Lih", "Teaching high school", new Integer JavaDoc(4), new Boolean JavaDoc(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 JavaDoc getColumnName(int col) {
42         return columnNames[col];
43     }
44
45     public Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc && !(value instanceof Integer JavaDoc)) {
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 JavaDoc(value.toString());
94                 fireTableCellUpdated(row, col);
95             }
96             catch (NumberFormatException JavaDoc 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 }
Popular Tags