I have a ArrayList and JTable load Object from ArrayList to display on table.
When i sort table by using method:


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:

After sort:

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;

    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;

    public Class getColumnClass(int columnIndex) {
        return classes[columnIndex];

    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!

6 Years
Discussion Span
Last Post by o0oKodako0o

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!
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.

viewRowIndex - the index of the row in the view
the index of the corresponding row in the model

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.