/Users/lyon/j4p/src/bookExamples/ch18Swing/TableData.java
|
1 package bookExamples.ch18Swing;
2
3 import javax.swing.table.AbstractTableModel;
4
5 /**
6 * Copyright DocJava, inc. User: lyon
7 * <p/>
8 * Date: Nov 6, 2004
9 * <p/>
10 * Time: 7:12:33 AM
11 */
12 public class TableData {
13 private String columnNames[];
14 private Object data[][];
15 private final GeneralTableModel tableModel = new GeneralTableModel();
16
17 public TableData(String columnNames[], Object data[][]) {
18 this.columnNames = columnNames;
19 this.data = data;
20 }
21
22 public GeneralTableModel getTableModel() {
23 return tableModel;
24 }
25
26 private class GeneralTableModel extends AbstractTableModel {
27 public int getColumnCount() {
28 return columnNames.length;
29 }
30
31 public int getRowCount() {
32 return data.length;
33 }
34
35 public String getColumnName(int col) {
36 return columnNames[col];
37 }
38
39 public Object getValueAt(int row, int col) {
40 return data[row][col];
41 }
42
43 /*
44 * JTable uses this method to determine the default renderer/
45 * editor for each cell. If we didn't implement this method,
46 * then the last column would contain text ("true"/"false"),
47 * rather than a check box.
48 */
49 public Class getColumnClass(int c) {
50 return getValueAt(0, c).getClass();
51 }
52
53 /*
54 * Don't need to implement this method unless your table's
55 * editable.
56 */
57 public boolean isCellEditable(int row, int col) {
58 //Note that the data/cell address is constant,
59 //no matter where the cell appears onscreen.
60 if (col < 2) {
61 return false;
62 } else {
63 return true;
64 }
65 }
66
67 /*
68 * Don't need to implement this method unless your table's
69 * data can change.
70 */
71 public void setValueAt(Object value, int row, int col) {
72 data[row][col] = value;
73 fireTableCellUpdated(row, col);
74 }
75 }
76 }
77
78