KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > util > AncientSwingTeam


1 package org.jdesktop.swing.util;
2
3 import javax.swing.table.AbstractTableModel JavaDoc;
4
5 /**
6  * Standard Table with class-Infos. Taken from some old
7  * SwingSet...
8  * Can remove/add rows.
9  */

10 public class AncientSwingTeam extends AbstractTableModel JavaDoc {
11
12     final String JavaDoc[] names = { "First Name", "Last Name", "Favorite Color",
13             " No.", "Vegetarian" };
14
15     final Object JavaDoc[][] data = {
16             { "Mark", "Andrews", "Red", new Integer JavaDoc(2), new Boolean JavaDoc(true) },
17             { "Tom", "Ball", "Blue", new Integer JavaDoc(99), new Boolean JavaDoc(false) },
18             { "Alan", "Chung", "Green", new Integer JavaDoc(838), new Boolean JavaDoc(false) },
19             { "Jeff", "Dinkins", "Turquois", new Integer JavaDoc(8), new Boolean JavaDoc(true) },
20             { "Amy", "Fowler", "Yellow", new Integer JavaDoc(3), new Boolean JavaDoc(false) },
21             { "Brian", "Gerhold", "Green", new Integer JavaDoc(0), new Boolean JavaDoc(false) },
22             { "James", "Gosling", "Pink", new Integer JavaDoc(21), new Boolean JavaDoc(false) },
23             { "David", "Karlton", "Red", new Integer JavaDoc(1), new Boolean JavaDoc(false) },
24             { "Dave", "Kloba", "Yellow", new Integer JavaDoc(14), new Boolean JavaDoc(false) },
25             { "Peter", "Korn", "Purple", new Integer JavaDoc(12), new Boolean JavaDoc(false) },
26             { "Phil", "Milne", "Purple", new Integer JavaDoc(3), new Boolean JavaDoc(false) },
27             { "Dave", "Moore", "Green", new Integer JavaDoc(88), new Boolean JavaDoc(false) },
28             { "Hans", "Muller", "Maroon", new Integer JavaDoc(5), new Boolean JavaDoc(false) },
29
30             { "Rick", "Levenson", "Blue", new Integer JavaDoc(2), new Boolean JavaDoc(false) },
31             { "Tim", "Prinzing", "Blue", new Integer JavaDoc(22), new Boolean JavaDoc(false) },
32             { "Chester", "Rose", "Black", new Integer JavaDoc(0), new Boolean JavaDoc(false) },
33             { "Ray", "Ryan", "Gray", new Integer JavaDoc(77), new Boolean JavaDoc(false) },
34             { "Georges", "Saab", "Red", new Integer JavaDoc(4), new Boolean JavaDoc(false) },
35             { "Willie", "Walker", "Phthalo Blue", new Integer JavaDoc(4),
36                     new Boolean JavaDoc(false) },
37
38             { "Kathy", "Walrath", "Blue", new Integer JavaDoc(8), new Boolean JavaDoc(false) },
39             { "Arnaud", "Weber", "Green", new Integer JavaDoc(44), new Boolean JavaDoc(false) } };
40
41     protected int rowCount = data.length;
42
43     public AncientSwingTeam() {
44
45     }
46
47     // public SwingTeam(int count) {
48
// rowCount = count;
49
// }
50

51     public int getColumnCount() {
52         return names.length;
53     }
54
55     public int getRowCount() {
56         return rowCount;
57     }
58
59     /** reuses values internally */
60     public Object JavaDoc getValueAt(int row, int col) {
61         // following shows only every second value
62
// if ((row + col) % 2 == 0) return null;
63
return data[row % data.length][col];
64     }
65
66     public void setValueAt(Object JavaDoc value, int row, int col) {
67         data[row % data.length][col] = value;
68         fireTableCellUpdated(row, col);
69     }
70
71     // The default implementations of these methods in
72
// AbstractTableModel would work, but we can refine them.
73
public String JavaDoc getColumnName(int column) {
74         return names[column];
75     }
76
77     /** returns class of column by asking class of value in first row. */
78     public Class JavaDoc getColumnClass(int c) {
79         Object JavaDoc value = null;
80         if (getRowCount() > 0) {
81             value = getValueAt(0, c);
82         }
83         if (value == null) {
84             return Object JavaDoc.class;
85         }
86         return value.getClass();
87     }
88
89     /** everything is editable. */
90     public boolean isCellEditable(int row, int col) {
91         return true;
92     }
93
94     /**
95      * insert length rows at rowIndex. PRE: rowIndex <= getRowCount()
96      */

97     public void insertRows(int rowIndex, int length) {
98         rowCount += length;
99         fireTableRowsInserted(rowIndex, rowIndex + length - 1);
100     }
101
102     /**
103      * remove rows. NOTE: not tested
104      */

105     public void removeRows(int rowIndex, int length) {
106         rowCount -= length;
107         if (rowCount < 0) {
108             length -= rowCount;
109             rowCount = 0;
110         }
111         fireTableRowsDeleted(rowIndex, rowIndex + length - 1);
112     }
113
114 } // end class SwingTeam
115
Popular Tags