Is there a way to derive a pruned AbstractTableModel from a full AbstractTableModel without getValueAt() complications?
My full data (including userIds) is loaded into a JTable AbstractTableModel. However, for display purposes, I wish to derive a pruned AbstractTableModel of data associated with a particular userId.
I am beginning to think this is not possible because getValueAt intervenes and throws IndexOutOfBounds exceptions? These exceptions seem to occur becuase the pruned Data is not populated.
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);
}
}
No comments:
Post a Comment