package tableModelListenerTest; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.DefaultCellEditor; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; public class TableEventHandle implements TableModelListener { JTable table = null; MyTable mt = null; JLabel label = null; public TableEventHandle() { JFrame f = new JFrame(); mt = new MyTable(); mt.addTableModelListener(this); table = new JTable(mt); JComboBox c = new JComboBox(); c.addItem("Taipei"); c.addItem("ChiaYi"); c.addItem("HsinChu"); table.getColumnModel().getColumn(1) .setCellEditor(new DefaultCellEditor(c)); table.setPreferredScrollableViewportSize(new Dimension(550, 30)); JScrollPane s = new JScrollPane(table); label = new JLabel("修改字段位置:"); f.getContentPane().add(s, BorderLayout.CENTER); f.getContentPane().add(label, BorderLayout.SOUTH); f.setTitle("TableEventHandle"); f.pack(); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void tableChanged(TableModelEvent e) { int row = e.getFirstRow(); int column = e.getColumn(); label.setText("修改字段位置:" + (row + 1) + "行" + (column + 1) + "列"); boolean cheat = ((Boolean) (mt.getValueAt(row, 6))).booleanValue(); int grade1 = ((Integer) (mt.getValueAt(row, 2))).intValue(); int grade2 = ((Integer) (mt.getValueAt(row, 3))).intValue(); int total = grade1 + grade2; if (cheat) { if (total > 120) mt.mySetValueAt(new Integer(119), row, 4); //只更新值,不触发事件。改为mt.setValueAt()也可。但会不断触发事件,java应该做了相应处理,否则会内存溢出。 else mt.mySetValueAt(new Integer(total), row, 4); mt.mySetValueAt(new Boolean(false), row, 5); } else { if (total > 120) mt.mySetValueAt(new Boolean(true), row, 5); else mt.mySetValueAt(new Boolean(false), row, 5); mt.mySetValueAt(new Integer(total), row, 4); } table.repaint(); } public static void main(String args[]) { new TableEventHandle(); } }
</pre><pre class="java" name="code">
package tableModelListenerTest; import javax.swing.table.AbstractTableModel; public class MyTable extends AbstractTableModel { /** * */ private static final long serialVersionUID = 1L; Object[][] p = { { "阿呆", "Taipei", new Integer(66), new Integer(32), new Integer(98), new Boolean(false), new Boolean(false) }, { "阿瓜", "ChiaYi", new Integer(85), new Integer(69), new Integer(154), new Boolean(true), new Boolean(false) } }; String[] n = { "姓名", "居住地", "语文", "数学", "总分", "及格" }; public int getColumnCount() { return n.length; } public int getRowCount() { return p.length; } public String getColumnName(int col) { return n[col]; } public Object getValueAt(int row, int col) { return p[row][col]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int rowIndex, int columnIndex) { return true; } public void setValueAt(Object value, int row, int col) { p[row][col] = value; fireTableCellUpdated(row, col); // 该方法触发TableModelEvent 事件,tableChanged()会进行相应的处理 } public void mySetValueAt(Object value, int row, int col) { p[row][col] = value; //fireTableCellUpdated(row, col); //此处不能有,否则会陷入对自身的调用,导致内存溢出。 } }