1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.swing;
17
18 import javax.swing.table.AbstractTableModel;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 /***
24 * A sample table model
25 *
26 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
27 * @version $Revision: 1.8 $
28 */
29 public class MyTableModel extends AbstractTableModel {
30
31 /*** The Log to which logging calls will be made. */
32 private static final Log log = LogFactory.getLog(MyTableModel.class);
33
34 public MyTableModel() {
35 }
36
37
38 final String[] columnNames = {
39 "First Name",
40 "Last Name",
41 "Sport",
42 "# of Years",
43 "Vegetarian"
44 };
45 final Object[][] data = {
46 {"Mary", "Campione",
47 "Snowboarding", new Integer(5), new Boolean(false)},
48 {"Alison", "Huml",
49 "Rowing", new Integer(3), new Boolean(true)},
50 {"Kathy", "Walrath",
51 "Chasing toddlers", new Integer(2), new Boolean(false)},
52 {"Mark", "Andrews",
53 "Speed reading", new Integer(20), new Boolean(true)},
54 {"Angela", "Lih",
55 "Teaching high school", new Integer(4), new Boolean(false)}
56 };
57
58 public int getColumnCount() {
59 return columnNames.length;
60 }
61
62 public int getRowCount() {
63 return data.length;
64 }
65
66 public String getColumnName(int col) {
67 return columnNames[col];
68 }
69
70 public Object getValueAt(int row, int col) {
71 return data[row][col];
72 }
73
74
75
76
77
78
79
80 public Class getColumnClass(int c) {
81 return getValueAt(0, c).getClass();
82 }
83
84
85
86
87
88 public boolean isCellEditable(int row, int col) {
89
90
91 if (col < 2) {
92 return false;
93 } else {
94 return true;
95 }
96 }
97
98
99
100
101
102 public void setValueAt(Object value, int row, int col) {
103 if (log.isDebugEnabled()) {
104 log.debug("Setting value at " + row + "," + col
105 + " to " + value
106 + " (an instance of "
107 + value.getClass() + ")");
108 }
109
110 if (data[0][col] instanceof Integer
111 && !(value instanceof Integer)) {
112
113
114
115
116
117
118
119 try {
120 data[row][col] = new Integer(value.toString());
121 fireTableCellUpdated(row, col);
122 } catch (NumberFormatException e) {
123 log.error( "The \"" + getColumnName(col)
124 + "\" column accepts only integer values.");
125 }
126 } else {
127 data[row][col] = value;
128 fireTableCellUpdated(row, col);
129 }
130 }
131
132
133 }