JTable, RowSorter, data getSelectedRow

I use RowSorter in JTable, and when I sort one of the columns and call the getSelectedRowData () method, it returns incorrect 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 the problem is that the sorter sorts the table data, but the ArrayList with the data does not reload. How can i fix this?

+3
source share
1 answer

To obtain the currently selected data, you can convert the selected line number to view the line number of the model.

jTable1.convertRowIndexToModel(jTable1.getSelectedRow())
+5
source

Source: https://habr.com/ru/post/1787284/


All Articles