不用defaultTableModel
例如:我自己定义的一个InfoTableModel extends AbstractTableModel
import javax.swing.table.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class InfoTableModel extends AbstractTableModel {
private Object[][] data ={ };
final String[] names = {"状态","保存文件名","URL","百分率","所需时间"};
// 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;
}
public void addRow(Object[] objects) {
//定义一个比现在Data数组多一行的数组,开辟内存空间来存放新的Record
Object[][] dataTemp = new Object[getRowCount() + 1][getColumnCount()];
//复制数组
System.arraycopy(data, 0, dataTemp, 0, getRowCount());
//添加最后那项记录
for (int i=0;
i< getColumnCount();i++) {
dataTemp [getRowCount()] = objects;
}
//赋值生成的新的数组
data = dataTemp ;
//产生事件通知Listener们
fireTableDataChanged() ;
//fireTableRowsInserted()也是可以的,系统开销更小
}
}
在你需要添加的地方,使用:
InfoTableModel aModel = (InfoTableModel)jInfoTable.getModel();
Object[] ssRows = {"run","d//",URLdlg.getURLstring(),"23%","0:23:32"};
aModel.addRow(ssRows);
jInfoTable.setModel(aModel);
jInfoTable.updateUI();