I am using a new class for a TableModel
that extends AbstractTableModel
, but when i try to add the header row it doesn't show. This is the code:
public class StudentTableModel extends AbstractTableModel {
private ArrayList<Student> student;
private ArrayList<Group> groups;
private int tableType=2;
public String []coluNamess = {
"id","name",
"last name",
" birth date",
"phone","adress",
"class"
};
public String []coluNamesg = {
"id","title",
"level","num"
};
public String getColumnName(int column ) {
if(tableType==1) {
return this.coluNamess[column];
} else {
return this.coluNamesg[column];
}
}
public int getColumnCount() {
if(tableType==1) {
return 7;
} else {
return 4;
}
}
public int getRowCount() {
if(tableType==1) {
return student.size();
} else {
return groups.size();
}
}
public String getValueAt(int row, int colu) {
if(tableType==1) {
Student studentm=student.get(row);
switch(colu) {
case 0:
return Double.toString((double)studentm.getId());
case 1:
return studentm.getFname();
case 2:
return studentm.getLname();
case 3:
return studentm.getB_date();
case 4:
return studentm.getPhone();
case 5:
return studentm.getAdress();
case 6:
return Double.toString((double)studentm.getGroupId());
}
} else if(tableType==2) {
Group group=groups.get(row);
switch(colu) {
case 0:
return Double.toString((double)group.getId());
case 1:
return group.getTitle();
case 2:
return group.getLevel();
case 3:
return Double.toString((double)group.getTime());
}
}
return null;
}
public void setStudent(ArrayList<Student> students) {
this.student= students;
}
public void setGroup(ArrayList<Group> groups) {
this.groups= groups;
}
public void setType(int type) {
this.tableType=type;
}
}
The header is not appearing but the content is shown. I don't know where the problem is!!!!
Add a table to a scroll pane, ie: new JScrollPane(table)
. See Adding a Table to a Container for more details:
The scroll pane automatically places the table header at the top of the viewport. The column names remain visible at the top of the viewing area when the table data is scrolled.
If you are using a table without a scroll pane, then you must get the table header component and place it yourself.