s_lavie
asked on
JTable repaint()
When I call the repaint() method of JTable, the row selection is vanished. I would like to restore the selection status.
Holding the selected rows indices is a bad idea, since their order might be changed after repainting (if the table is sorted, for example).
Any idea?
Holding the selected rows indices is a bad idea, since their order might be changed after repainting (if the table is sorted, for example).
Any idea?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Okay, no more calling to repaint() and the selection is not vanished, but the wrong line is now selected, i.e. for example I have a table like this:
0 a
1 b
2 c
3 d
Now, suppose row index 1 is selected (1 b), when I sort the table it looks like this:
3 d
2 c
1 b
0 a
And again row index 1 is selected (2 c), whereas row number 3 is supposed to be selected ((1 b) as before).
Now, what do I miss here, and how I fix it?
0 a
1 b
2 c
3 d
Now, suppose row index 1 is selected (1 b), when I sort the table it looks like this:
3 d
2 c
1 b
0 a
And again row index 1 is selected (2 c), whereas row number 3 is supposed to be selected ((1 b) as before).
Now, what do I miss here, and how I fix it?
ASKER
In row number 3 I meant row index 2
How's your sort implemented?
ASKER
In MyTableModel (that extends DefaultTableModel) I have a sort method:
public void sortTable(JTable table, Comparator comparator)
{
for (int i = 0; i < getColumnCount(); i++)
{
TableColumn column = colModel.getColumn(i);
column.setHeaderValue(getC olumnName( column.get ModelIndex ())); // getColumnName() is overriding the getColumnName() in DefaultTableModel.
}
table.getTableHeader().rep aint();
Collections.sort(dataVecto r, comparator);
}
public void sortTable(JTable table, Comparator comparator)
{
for (int i = 0; i < getColumnCount(); i++)
{
TableColumn column = colModel.getColumn(i);
column.setHeaderValue(getC
}
table.getTableHeader().rep
Collections.sort(dataVecto
}
You're not firing any event informing listenerer that you have changed the model.
and another thing - the overall algorithm should be:
- save the selected row id
- sort
- find the new index for the row with saved id
- select this index.
- save the selected row id
- sort
- find the new index for the row with saved id
- select this index.
I actually think the selection model listens to the table model, and updates itself. (But I could be wrong).
> I actually think the selection model listens to the table model
I agree, but when you sort the table the only possible event to throw is
TableModelEvent(source, 0, max, ALL_COLUMNS, UPDATE);
and SelectionModel will use the same selection index.
I agree, but when you sort the table the only possible event to throw is
TableModelEvent(source, 0, max, ALL_COLUMNS, UPDATE);
and SelectionModel will use the same selection index.
ASKER
I added the line:
table.tableChanged(new TableModelEvent(this, 0, table.getRowCount() - 1, TableModelEvent.ALL_COLUMN S, TableModelEvent.UPDATE));
After the line:
Collections.sort(dataVecto r, comparator);
> - save the selected row id
> - sort
> - find the new index for the row with saved id
> - select this index.
That's exactly what I was afraid of:
Find the new index for the row with saved id is quiet heavy isn't it? it means for every selected row I have to run over the entire table!
table.tableChanged(new TableModelEvent(this, 0, table.getRowCount() - 1, TableModelEvent.ALL_COLUMN
After the line:
Collections.sort(dataVecto
> - save the selected row id
> - sort
> - find the new index for the row with saved id
> - select this index.
That's exactly what I was afraid of:
Find the new index for the row with saved id is quiet heavy isn't it? it means for every selected row I have to run over the entire table!
> That's exactly what I was afraid of:
you have to find out the new index for the selected row somehow - there isn't magic solution :)
you have to find out the new index for the selected row somehow - there isn't magic solution :)
ASKER
Do you have any intelligent solution for saving and finding those indices?
ok, let's define the REAL problem:
- you have collection of Objects;
- you rearrange (sort) the items in the collection;
- you want to find the new index for some item, given the old index.
I cannot think of any good solution right now, but maybe somebody else will be able to help you
- you have collection of Objects;
- you rearrange (sort) the items in the collection;
- you want to find the new index for some item, given the old index.
I cannot think of any good solution right now, but maybe somebody else will be able to help you
hey, thanks for the points !
ASKER
You earned them!
> the row selection is vanished.
Then the row must no longer be selected.