Search code examples
javaswingsortingjtable

JTable, RowSorter, getSelectedRow data


I'm using RowSorter in JTable and when I sort one of the column and call the method getSelectedRowData() it return wrong data.

public Object getSelectedRowData()
{
    if(getDataArray() != null)
    {
        if(grid.getRowSorter() != null)
            return ((GridModel) grid.getRowSorter().getModel()).getData().get(grid.getSelectedRow());
        else
            return model.getData().get(grid.getSelectedRow());
    }
    else
        return null;
}

I think that the problem is that the sorter sort the table data but the ArrayList with data isn't reloaded. How I can fix it?


Solution

  • For getting current selected data you can convert selected row number in view to model row number.

    jTable1.convertRowIndexToModel(jTable1.getSelectedRow())