I have an ArrayList of type modules, each module has an arraylist of Assignments. I have written the following TableModel but when the something is selected in the table I have a problem and it causes my ArrayLists to be out of bounds. Here is my table model:
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;
}
}
As you can see I store the moduleID as global variables which is fine when its first started, but once something is selected it uses the last values. What else can I do to stop this from happening?