0

I have 1 window with a collection of students and 1 window where I can add a student. As default I've added 1 row to the table by doing:

DefaultTableModel tableModel = (DefaultTableModel) studentsTable.getModel();
tableModel.addRow(new Object[]{"Sir Student", "M", "email@email.com", "23-04-1921"});    

So when I save the student in the other frame I do the following:

studentsFrame overview = new studentsFrame();
overview.addStudent(student);

Where the student is a new Student() object with some information about a student.

Well then I'm trying to add the student to the JTable but this doesn't work. I'm not sure what's wrong. When I call this in my constructor is does work. But I want to add it dynamically.

public void addStudent(Student student) {
    tableModel.addRow(new Object[]{"New student", "M", "email@new.com", "19-09-1955"});
}

I'm not sure what's going wrong..

guidsen
  • 2,333
  • 6
  • 34
  • 55
  • 1
    Please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that exhibits the problem you describe; this [example](http://stackoverflow.com/a/19472190/230513) may guide you. – trashgod Sep 12 '14 at 18:37
  • Based on the lack of code provided I would guess that you have two references to the "tableModel" variable. One that is defined as an instance variable (but was not added to the table) and the other is defined as a local variable which is how you populated the table with the initial record. Get rid of the local variable. – camickr Sep 12 '14 at 18:39
  • I have it as a property in the `studentsFrame`: `private static DefaultTableModel tableModel;` – guidsen Sep 12 '14 at 18:57

1 Answers1

0

I called indeed a new instance of the object so it overwrited the already made DefaultTableModel. I've deleted it and made a static function. It's working now.

guidsen
  • 2,333
  • 6
  • 34
  • 55