So, I am currently building the GUI for my program. I am using JTables with a custom AbstractTableModel to fill them with the contents of my different LinkedLists. However, if I add a JTable to my GUI class, and initialize the LinkedList class there, then that is not the LinkedList that I need. I need the LinkedList Studentlist, which I have stored in my Database class. How do I tell the table that? I can't really show any GUI code, because it is all still in the making, and I am just testing things out at the moment.
My other question is, this is my AbstractTableModel:
public class StudentTableModel extends AbstractTableModel {
public static final String[] columnNames = { "ID", "First Name",
"Last Name" };
private LinkedList<Student> data;
public StudentTableModel(LinkedList<Student> data) {
this.data = data;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public int getRowCount() {
return data.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Student student = data.get(rowIndex);
if (student == null) {
return null;
}
switch (columnIndex) {
case 0:
return student.getID();
case 1:
return student.getFirstname();
case 2:
return student.getLastname();
default:
return null;
}
}
}
Do I need to add anything to the code to make the table automatically update when a student is added to the list?
Thanks in advance.