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!

This is the expected behaviour. What exactly is the problem you are trying to solve?

This is the expected behaviour. What exactly is the problem you are trying to solve?

I want after table is sorted, the model is apply for the ArrayList!
Example:
in ArrayList i have two Object: "2" and "1" (in Integer)
display on table by setModel(ArrayList) --> "2" at first row and "1" at the second row
after sort, --> "1" at first row, "2" at second row

when i choose the first row after sort, and write code to show Object in that row in a popup. and the popup's not display "1", it display "2"! So that's problem!

OK, now I understand.
oOoKodakoOo please meet convertRowIndexToModel
convertRowIndexToModel pleased meet oOoKodakoOo

No, seriously

public int convertRowIndexToModel(int viewRowIndex)

Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.

Parameters:
viewRowIndex - the index of the row in the view
Returns:
the index of the corresponding row in the model

Thank you very much! i've solved!
u've saved me! :D:D
Yeah yeah! :D:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.