javaswingarraylistjtabledefaulttablemodel

Can not display contents of Arraylist n JTable


I can't figure this out (have been trying to fix this for the past 2-3 hours). I would like to display the contents of an arraylist, but they do not appear in the table and also there are NO errors, they simply do not appear. Here is my code:

private class examinations{
private int id;
private int candidate_id;
private String date;
private String exam;
private String examNumber;

public examinations(int id, int student_id, String date, String exam, String examNumber) {
    this.id = id;
    this.student_id = student_id;
    this.date = date;
    this.exam= exam;
    this.examNumber= examNumber;
}

public ArrayList ListExams(){
    ArrayList<exams> list = new ArrayList<exams>();

    return list;
}

public void addRollToTable(){
    DefaultTableModel model = (DefaultTableModel)tableExams.getModel();
    ArrayList<exams> list = ListExams();
    Object rowData[] = new Object[5];
    for(int i = 0; i < list.size(); i++) {
        rowData[0] = list.get(i).id;
        rowData[1] = list.get(i).student_id;
        rowData[2] = list.get(i).date;
        rowData[3] = list.get(i).exam;
        rowData[4] = list.get(i).examNumber;

        model.addRow(rowData);
    }
}

} I tested this loop and the variables coming out of the other list are there, so a System.out.println(list.get(i).exam); will display the correct thing i typed. However the table will NOT display whatever I add in the rowData. It gives, again, no errors. Let me show you the DefaultTableModel code. This code is in the private void initComponents() of my class...

    Object [][] data = {};
    String[] columnNames = {"Id", "Student_Id", "Date", "Exam", 
    "Exam_number"};
    tableExams= new javax.swing.JTable();

    DefaultTableModel model = new DefaultTableModel(data, columnNames);
    tableExams.setModel(model);
    tableExams.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
    jScrollPane4.setViewportView(tableExams);

I've been reading this: DefaultTableModel Class Overview But I still can't find where I am going wrong... Could anyone give a tip?


Solution

  • OK, I solved it, even though this was just a work around, I'd accept it.

    All I did was use this.setVisible(false) and then entered the information in the other JFrame. Clicking add, i make an object of the first JFrame, passed all the variables, used this.dispose() and then called .setVisible(true) to return to the table, which displayed the information. LOL that was a long testing and re-writing of code to actually realize it was something that small...

    I am sorry, I did not know where the actual problem was, and yeah thanks a lot for that simple suggestion there camickr. I tried it in the same JFrame and it worked, then I tried it between 2 JFrames and I realized the JFrame with the table DID NOT update the table. repaint() also didn't work. You quite literally helped me out with that small tip, which is all i needed. THANKS!!!!!!