我会在黑暗中拍摄并猜测你想要完成这样的事情:
DefaultTableModel dtm = (DefaultTableModel)myJTable.getModel();
for (MyRowObject row : webSiteDownloader.getWebSites()) {
dtm.insertRow(0, row.toArray());
}
您使用 insertRow 而不是 addRow 是否有特殊原因?
另外,我真的很想建议您通过扩展 AbstractTableModel 来推出自己的特殊用途 TableModel。基本未经测试的例子:
public class MyTableModel extends AbstractTableModel
{
protected List<MyObject> rows;
public MyTableModel()
{
rows = new ArrayList<MyObject>();
}
public void add(MyObject obj)
{
rows.add(obj);
}
@Override
public int getRowCount()
{
return rows.size();
}
@Override
public int getColumnCount()
{
// This value will be constant, but generally you'd also
// want to override getColumnName to return column names
// from an array, and in that case you can return the length
// of the array with column names instead
return 2;
}
@Override
public Object getValueAt(int row, int column)
{
MyObject obj = rows.get(row);
// Change this to match your columns
switch(column) {
case 0: return obj.getId();
case 1: return obj.getName();
}
return null;
}
}