我有一个带有 HashMap 元素的 vector 。 我想把它放在一个表中,每个 HashTable 值都必须在具有 HashTable 键列标题的列中。 因此具有键“key1”的元素必须出现在名称为“key1”的表列中。
问题是,当我尝试使用setHash()
函数添加/删除表的列时。
我传递了一个包含更多/更少元素的 String[],当此函数运行 fireTableStructureChanged()
时,java 疯狂抛出。
我不明白问题出在哪里。你能帮帮我吗?
表模型的实现在这里:
public class ResizableTableModel extends AbstractTableModel {
protected DataSource src;
protected String[] hash;
//......................
public void setHash(String[] hash) {
this.hash = hash;
fireTableStructureChanged(); // THROWS!
}
public ArrayList getData() { return src.getData(); }
public int getColumnCount() { return hash.length; }
public int getRowCount() { return getData() == null ? 0 : getData().size(); }
public String getColumnName(int col) { return hash[col]; }
public boolean isCellEditable(int row, int col) { return true; }
public Object getValueAt(int row, int col) {
try {
return ((HashMap) getData().get(row)).get(hash[col]);
} catch (Exception e) {
return null;
}
}
public void setValueAt(Object obj, int row, int col) {
try {
//...................
} catch (Exception e) {}
fireTableDataChanged();
}
}
最佳答案
不要从 XxxTableModel 中删除列,
删除 Column from JTables仅查看,数据仍然是stored and accesible in XxxTableModel
关于java - 从自定义 AbstractTableModel 添加/删除更多列到 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920170/