我有一个类型模块的ArrayList,每个模块都有一个Assignments的arraylist。我已经编写了以下TableModel,但是当在表中选择某些内容时,我遇到了问题,它导致ArrayLists超出范围。这是我的表格模型:
public class AssignmentsTableModel extends AbstractTableModel {
private ArrayList<Module> modules;
private static final String[] COLUMN_NAMES = {"Module Identifier", "Module Name", "Assignment Title", "Author", "Date Set", "Date Due", "Weighting"};
private int moduleID;
private int assignmentID;
private Module module;
private int totalNumberAssignments;
public AssignmentsTableModel(ArrayList<Module> modules) {
moduleID = 0;
assignmentID = 0;
this.modules = modules;
module = modules.get(moduleID);
totalNumberAssignments = getRowCount();
}
public AssignmentsTableModel(Module module) {
modules = new ArrayList<Module>();
modules.add(module);
}
@Override
public int getRowCount() {
int rowCount = 0;
for(Module mod : modules){
rowCount += mod.getAssignments().size();
}
return rowCount;
}
@Override
public int getColumnCount() {
return COLUMN_NAMES.length; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public Object getValueAt(int row, int column) {
if(column%getColumnCount() == 0 && getRowCount() <= totalNumberAssignments){
if(isAtLastAssignment(row, moduleID)){
assignmentID = 0;
moduleID += 1;
}
}
module = modules.get(moduleID);
ArrayList<Assignment> assignments = module.getAssignments();
Assignment assignment = assignments.get(assignmentID);
switch (column){
case 0: return module.getIdentifier();
case 1: return module.getTitle();
case 2: return assignment.getTitle();
case 3: return assignment.getAuthor();
case 4: return assignment.getSet();
case 5: return assignment.getDue();
case 6: return assignment.getWeighting();
default: return null;
}
}
public String getColumnName(int columnIndex) {
return COLUMN_NAMES[columnIndex];
}
public boolean isAtLastAssignment(int row, int moduleID){
boolean atLast = false;
ArrayList<Assignment> assignments = modules.get(moduleID).getAssignments();
if(moduleID == 0){
if(row == assignments.size()){
atLast = true;
}
} else {
int assignmentsSize = assignments.size() + getCurrentRowNumber(moduleID);
if(row == assignmentsSize){
atLast = true;
}
}
return atLast;
}
public int getCurrentRowNumber(int moduleID){
int rowNumber = 0;
for(int i = 0; i < moduleID; i++){
rowNumber = modules.get(i).getAssignments().size()-1;
}
return rowNumber;
}
}
如您所见,我将moduleID存储为全局变量,这在第一次启动时很好,但是一旦选择了某个内容,它就会使用最后一个值。我还能做些什么来阻止这种情况的发生?
最佳答案
每当表格分析器想要获取单元格的值以便可以对其进行呈现时,表渲染器就会调用TableModel方法getValueAt()。它与(直接或可靠地)与您单击表行并将表标记为选定行无关。
换句话说,您不希望由于调用getValueAt()
方法而改变表模型的状态。
如果您的getValueAt()
方法使用该行作为数组的索引,那将是最简单的。但是,看起来每个模块的行数都是可变的。在这种情况下,如果您无法通过算法生成正确的索引,则可以在构造函数中一次“平化”数据,并简化查找代码。
也就是说,您的表模型包含:
private List<RowData> rowData
还有一个内部类,其中包含行数据:
private static class RowData
{
private Moddule;
private Assignment assignment;
...
}
在您的构造函数中:
public AssignmentsTableModel(List<Module> modules)
{
this.rowData = new ArrayList<Module>();
for (Module module : modules)
{
for (Assignment assignment : module.getAssignments())
{
rowData.add(new RowData(module, assignment));
}
}
}
这简化了您的
getValueAt()
方法:@Override
public Object getValueAt(int row, int column) {
final RowData row = this.rowData.get(row);
switch (column){
case 0: return row.getModule().getIdentifier();
case 1: return row.getModule().getTitle();
case 2: return row.getAssignment().getTitle();
case 3: return row.getAssignment().Author();
case 4: return row.getAssignment().getSet();
case 5: return row.getAssignment().getDue();
case 6: return row.getAssignment().getWeighting();
default: return null;
}
}