-1

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sky Mage
  • 31
  • 5

2 Answers2

1

First of all learn and use Java naming conventions:

  1. Classs names SHOULD start with an upper case character. Can you show me a class in the JDK that does not?

  2. Method should should NOT start with an upper case character. Again, can you show me a method in the JDK the does?

Learn by example and don't make up your own conventions.

so a System.out.println(list.get(i).exam); will display the correct thing i typed

I don't know how this is possible. Your code is as follows:

a) First, you retrieve the ArrayList from the "listExams() method.

ArrayList<exams> list = ListExams();

b) But in the "listExams()" method all you do is create an empty ArrayList.

ArrayList<exams> list = new ArrayList<exams>();

So you are missing the logic that actually adds data to the ArrayList.

Based on the logic provided, you don't even need the ArrayList. Just take the data from the Examination class and add it to the TableModel:

Object rowData[] = new Object[5];
rowData[0] = id;
rowData[1] = student_id;
rowData[2] = date;
rowData[3] = exam;
rowData[4] = examNumber;
model.addRow(rowData);

For a different solution you could create a custom TableModel to hold your "Examination" objects. Check out Row Table Model for a step-by-step example of how this can be done.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • The problem isn't really in the fact that i am creating an empty array, because even when I put object in there, and as I said, they all return a result when I do the print test. The issue is they do not show on the table. I created a custom table model, still, they don't show, but when I do `System.out.println(model.getRowCount())`, it prints "1" (butthe table doesn't display the 1 row i added). There are no errors, just like before. – Sky Mage Dec 15 '17 at 10:05
  • Post a proper [mcve] that demonstrates the problem. – camickr Dec 15 '17 at 15:49
  • @SkyMage You were asked to post your example demonstrating the problem. I can't possibly guess what you are doing wrong. If your method has a reference to the TableModel of the visible JTable, then the addRow(...) method will work. – camickr Dec 16 '17 at 18:11
  • Ok so let me just wrap it all up - The problem is that whenever I want to add a row in this table, but when passing the values from ANOTHER JFrame, it does not display the new record. Here: [link] (https://image.prntscr.com/image/HXHLZg84SbCJYKk_0HSAlA.png) – Sky Mage Dec 16 '17 at 18:25
  • I guess you really don't want help. An image tells us nothing. Without a proper [mcve] you are on your own. We don't have time to guess what you are doing. So what you need is a simple JFrame with a JTable and a button. When you click the button you add a row of hard coded data to the table. Learn how this basic process works and then apply the knowledge to your real application. This will take 20-30 lines of code to test so it will be easy for us to help is you still have a problem. – camickr Dec 16 '17 at 18:27
-1

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!!!!!!

Sky Mage
  • 31
  • 5
  • (1-) No, this is NOT a solution. A table will automatically repaint itself when the data in the TableModel is changed. You still have some fundamental design problem. Also, you should NOT be using a second JFrame. Application should only have a single JFrame. For child windows you should be using a JDialog to prompt for Exam information. `and yeah thanks a lot for that simple suggestion there camickr.` - then that is the answer that should be "accepted", or at least a simple "thank yout" to acknowledge the time spent helping. – camickr Dec 18 '17 at 15:21