有没有办法从完整的 AbstractTableModel 派生一个修剪过的 AbstractTableModel 而没有 getValueAt() 复杂性?
我的完整数据(包括 userIds)被加载到 JTable AbstractTableModel 中。但是,出于显示目的,我希望派生与特定 userId 关联的数据的修剪 AbstractTableModel。
我开始认为这是不可能的,因为 getValueAt 干预并引发 IndexOutOfBounds 异常?这些异常似乎是因为未填充修剪后的数据。
public class PrunedUserIdTableModel extends AbstractTableModel {
TableModel fullModel;
List columnIdentifiers;
List tempDatum;
List tempData; // holds tempDatums
int rowCount; // reports pruned rowCount through getRowCount() method
List prunedData; // intended to hold data of matched userId rows
public PrunedUserIdTableModel(JTable fullTable, String userId) {
fullModel = fullTable.getModel();
columnIdentifiers = new ArrayList();
tempDatum = new ArrayList();
tempData = new ArrayList();
rowCount = 0;
List<Integer> userCount = new ArrayList<>();
// Load columnIdentifiers from fullModel; omitted here
// Go through fullModel searching for rows with matching userIds
for (int i = 0; i < fullModel.getRowCount(); i++) {
for (int k = 0; k < fullModel.getColumnCount(); k++) {
tempDatum.add(fullModel.getValueAt(i,k);
if ((fullModel.getValueAt(i,k).equals(userId)) {
// Matching userId found; record relevant row
userCount.add(g);
}
}
tempData.add(tempDatum);
tempDatum.clear();
}
// Now populate prunedData
for (int j = 0; j < userCount.size(); j++) {
prunedData.add(tempData.get(userCount.get(j)));
rowCount=rowCount+1;
}
fireTableChanged(null);
}
@Override
public int getRowCount() {
return rowCount;
}
@Override
public int getColumnCount() {
return fullModel.getColumnCount();
}
@Override
public int getValueAt(int rowIndex, int columnIndex) {
// THROWS INDEX OUT OF BOUNDS EXCEPTION: Index 0; size 0
List rowList = (List)prunedData.get(rowIndex);
return rowList.get(columnIndex);
}
}