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 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 55 public Class getColumnClass(int c) { 56 return getValueAt(0, c).getClass(); 57 } 58 59 63 public boolean isCellEditable(int row, int col) { 64 if (col < 2) { 67 return false; 68 } 69 else { 70 return true; 71 } 72 } 73 74 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 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 } | Popular Tags |