Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7718941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:17:28+00:00 2026-06-01T03:17:28+00:00

I have an ArrayList of type modules, each module has an arraylist of Assignments.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:17:29+00:00Added an answer on June 1, 2026 at 3:17 am

    The TableModel method getValueAt() is called by the table renderer each time it wants to get the value of the cell so that it can render it. It’s not (directly or reliably) related to you clicking on the table row and having the table mark the row as selected.

    In other words, you don’t want to be mutating the state of your table model as a result of calling the getValueAt() method.

    It would be easiest if your getValueAt() method use the row as the index into your array. However, it looks like you have a variable number of rows for each module. In that case, if you can’t algorithmically generate the right index, you could “flatten” the data once in the constructor, and simplify the look-up code.

    That is, your table model contains:

    private List<RowData> rowData
    

    and an inner class which contains the row data:

    private static class RowData
    {
      private Moddule;
      private Assignment assignment;
    
      ...
    }
    

    And in your constructor:

    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));
        }
      }
    }
    

    This simplifies your getValueAt() method:

    @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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an arraylist that contains items called Room. Each Room has a roomtype
I have an arraylist that gets different type of values in it, 1st value->
I have an ArrayList<String> that I'd like to return a copy of. ArrayList has
I have an arraylist which has a class datastructure. public class empRec { public
I have an ArrayList of type String that contain certain values. I want to
I have some arraylist with different objects of the same interface type, e.g: Interface
If I have a type ArrayList<HashMap<String,String>> keys = functionWhichReturnsThisType(); How can I iterate through
I have java API which return this type: ArrayList[ArrayList[String]] = Foo.someJavaMethod() In scala program,
I have an ArrayList of type GeoPoint. private List<GeoPoint> points = new ArrayList<GeoPoint>(); I
I have a ArrayList of type BookingData to List<BookingData> ? I am using .net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.