0

I need some help with getValue method

Im reading from a file and storing the values in a

 public List<Object[]> students;

Now I want to add all of this values to my JTable

 @Override
    public Object getValueAt(int row, int col) {
        //return data[row][col];
        // Here I have to get data from students
    }

Usually from a lot of example I saw that they use Object[][] data for doing this kind of thing this could be something like this

//return data[row][col];

but since I read from a file I want List<Object[]> students;

Any Idea how to implement this in getValueAt method?

my solution will be this

@Override
    public Object getValueAt(int row, int col) {
        //return data[row][col];
        for(Object[] j: students)
        {
            return j[col];
        }
        return null;
    }

but this will only take the first object and will assign to all rows in my JTable

Nicholas
  • 3,529
  • 2
  • 23
  • 31

1 Answers1

8

how about:

@Override
public Object getValueAt(int row, int col) {
    return students.get(row)[col];
}
jmrah
  • 5,715
  • 3
  • 30
  • 37
  • bingo ))) Wow I tried the .get(), but never thought that I could put .get()[] thank you man!!! I will mark it as answer in 8 minutes – Nicholas Nov 09 '14 at 17:50
  • 2
    @Nicholas: It's a simple shortcut for `Object[] student = students.get(row); return student[col];` But everything would be much cleaner and simpler if students was a `List` rather than a `List`. – JB Nizet Nov 09 '14 at 17:52
  • Yeah, the problem was that Im doing a laboratory work, and the teacher gave us an Template and I could not change List[] into List or something like that, that was the rule. But anyway thank you, now I know!! – Nicholas Nov 09 '14 at 17:54