0

I have a JTable using AbstractTableModel where I activated the sort with :

table.setAutoCreateRowSorter(true);

I have my own insert and delete functions which were working fine as long as the table is not sorted.

private void insertLine(JTable t)
{
    int posInsert = t.getSelectedRow();
    Group v = new Group("group", "name","val");
    list.add(pos, v);
    GroupTableModel model = (GroupTableModel) t.getModel();
    model.fireTableRowsInserted(posInsert + 1, posInsert + 1);
}

private void deleteLine(JTable t)
{
    int posDel = t.getSelectedRow();
    if (posDel != -1)
    {
        list.remove(pos);
        GroupTableModel model = (GroupTableModel) t.getModel();
        model.fireTableRowsDeleted(posDel, posDel);
    }
}

list is an ArrayList<Group> which contain the data displayed in the JTable

When the table is sorted, it delete the wrong lines, and insert in the wrong position (not the selected+1). I tried several combinations using convertRowIndexToModel and convertRowIndexToView without success. The point is to use the right index when deleting from list, and to have a correct display in view side...

EDIT : After debugging, the issue was with the usage of convertRowIndexToView. the selectedRow should be used to fire the change not convertRowIndexToView. Now I have correct content when updating my method like below (same logic for delete) :

private void insertLine(JTable t)
{
    int posInsert = t.getSelectedRow();
    int modelPosInsert = t.convertRowIndexToModel(posInsert);
    Group v = new Group("group", "name","val");
    list.add(modelPosInsert, v);
    GroupTableModel model = (GroupTableModel) t.getModel();
    model.fireTableRowsInserted(posInsert + 1, posInsert + 1);
}

Now the issue is after the change, the sort is partially lost. Only few lines are changed in random positions ?

Djoz
  • 161
  • 2
  • 9
  • 2
    `" I tried several combinations using convertRowIndexToModel and convertRowIndexToView without success."` -- yes, this is what you will need. As far as I understand it, sorting changes the order in the display but not the order within the underlying model. – Hovercraft Full Of Eels Oct 12 '16 at 14:09
  • 1
    Consider creating and posting a small [mcve] where you show us your attempt to use the convert methods above. – Hovercraft Full Of Eels Oct 12 '16 at 14:09
  • Hello Hovercraft, I added an EDIT, the issue was model.fireTableRowsInserted(viewPos + 1, viewPos + 1); When I don't use convertRowIndexToView the result is correct but a display problem remains – Djoz Oct 12 '16 at 15:19
  • 1
    `model.fireTableRowsInserted();` - yes that would be an issue. You should not invoke the method outside the TableModel. It is the responsibility of the TableModel to invoke that method. So you should be adding a method to your custom TableModel to do the "insert" and "delete" functions. The data should only be contained in the TableModel not in List defined outside the TableModel. – camickr Oct 13 '16 at 00:18
  • This is just a simplification of my code. The data are in a List inside my TableModel which is GroupTableModel. And it's GroupTableModel that fire the TableRowsInserted. I resolved my refresh issue by replacing model.fireTableRowsInserted(posInsert + 1, posInsert + 1); with model.fireTableDataChanged(); but I don't have an explanation why the first one does not work ! – Djoz Oct 13 '16 at 07:56

0 Answers0