实现AbstractTableModel,重写里面的方法, 下面是JDK 自带的例子。详细请见 %jdkpaht%\demo\jfc\tableexampleTableModel dataModel = new AbstractTableModel() { // These methods always need to be implemented. public int getColumnCount() { return names.length; } public int getRowCount() { return data.length;} public Object getValueAt(int row, int col) {return data[row][col];} // The default implementations of these methods in // AbstractTableModel would work, but we can refine them. public String getColumnName(int column) {return names[column];} public Class getColumnClass(int col) {return getValueAt(0,col).getClass();} //将希望可修改的列设为可修改 public boolean isCellEditable(int row, int col) {return (col==4);} public void setValueAt(Object aValue, int row, int column) { data[row][column] = aValue; } };用如下方法即可取得相应数据 JTable.getModel() .getValueAt(row,column)
但用getValueAt取到的还是构造JTable的数组的值,不是重新填进去的值
为什么啊?
自带的例子。详细请见 %jdkpaht%\demo\jfc\tableexampleTableModel dataModel = new AbstractTableModel() {
// These methods always need to be implemented.
public int getColumnCount() { return names.length; }
public int getRowCount() { return data.length;}
public Object getValueAt(int row, int col) {return data[row][col];} // The default implementations of these methods in
// AbstractTableModel would work, but we can refine them.
public String getColumnName(int column) {return names[column];}
public Class getColumnClass(int col) {return getValueAt(0,col).getClass();}
//将希望可修改的列设为可修改
public boolean isCellEditable(int row, int col) {return (col==4);}
public void setValueAt(Object aValue, int row, int column) {
data[row][column] = aValue;
}
};用如下方法即可取得相应数据
JTable.getModel() .getValueAt(row,column)
valueChanged(ListSelectionEvent e)方法,加入你的想法就可以了.
或者在你定义JTable的时候这样定义
JTable t=new JTable(){
public void valueChanged(ListSelectionEvent e) {
//加入你的想法
}
}
你试试,应该能满足你的要求.