I have a ArrayList and JTable load Object from ArrayList to display on table.
When i sort table by using method:
table.setAutoCreateRowSorter(true);
by using that method, i can sort table by clicking on header of the column i want to sort! But after sort, the Object of row which i choose is different from the Object in ArrayList.
You can see two picture of my project
Before sort:
http://img521.imageshack.us/img521/7994/beforepx.png
After sort:
http://img837.imageshack.us/img837/2792/afterea.png
Class Student:
public class Student implements Serializable {
private String id;
private String name;
private int year;
private String genre;
private String course;
private String group;
private String falcuty;
public Student() {
}
public Student(String id, String name, int year, String genre, String course, String group, String falcuty) {
this.id = id;
this.name = name;
this.year = year;
this.genre = genre;
this.course = course;
this.group = group;
this.falcuty = falcuty;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public String getFalcuty() {
return falcuty;
}
public void setFalcuty(String falcuty) {
this.falcuty = falcuty;
}
@Override
public String toString() {
return id +" "+ year + " " + genre + " " + course + " " + group + " " + falcuty;
}
}
Class TableModel:
public class TableModel extends AbstractTableModel{
private String names[] = {"MSSV", "Họ tên", "Năm sinh", "Giới tính", "Khóa", "Lớp", "Khoa/Viện"};
private Class classes[] = {String.class, String.class, Integer.class, String.class, String.class, String.class, String.class};
ArrayList<Student> listStudent;
public TableModel(ArrayList<Student> listStudent) {
this.listStudent = listStudent;
}
public int getRowCount() {
return listStudent.size();
}
public int getColumnCount() {
return names.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
switch(columnIndex) {
case 0 :
return listStudent.get(rowIndex).getID();
case 1 :
return listStudent.get(rowIndex).getName();
case 2 :
return listStudent.get(rowIndex).getYear();
case 3 :
return listStudent.get(rowIndex).getGenre();
case 4 :
return listStudent.get(rowIndex).getCourse();
case 5 :
return listStudent.get(rowIndex).getGroup();
case 6 :
return listStudent.get(rowIndex).getFalcuty();
default :
return null;
}
}
@Override
public Class getColumnClass(int columnIndex) {
return classes[columnIndex];
}
@Override
public String getColumnName(int column) {
return names[column];
}
}
I think, because of the model i set for table is Student ArrayList so after sort, the model(ArrayList) is constant but Object displayed on table is arranged, not the same index.
ArrayList<Student> listStudent;
table.setModel(new TableModel(listStudent));
so somebody help me!! please!