views:

374

answers:

1

Hi,

I have a JTable (extended) and an implementation of TableModel, in which I have the methods ...

  • moveEntryUp(int rowIdx)
  • moveEntryDown(int rowIdx)

... within the table model implementation. I have verified that these work properly.

However, I am having problems in getting the changes made in the table model to propagate to the actual JTable. In this table I only allow single row selection, and have seperate button for Up and Down. When a row is elected, I need to be able to move that row up and down, and preserve the selection.

E.g.
If my rows are [A, B, C(selected)], and I press the up button,
I should get [A, C(selected), B].

The various approaches I have tried so far either fail to refresh the values in the JTable, or lose the selection.


NOTE:
I am hesitant to use a solution which requires an explicit call to repaint().
Assume that all cells in the JTable are not editable.

+2  A: 

Answering own question:

int row = mTable.getSelectedRow();
mTableModel.moveEntryUp(row);
mTable.getSelectionModel().setSelectionInterval(row -1, row -1);

Where mTable is the JTable, and mTableModel is my table model implementation, and is equivalent to mTable.getModel() (and typecast).

Should've poked around longer before asking!

bguiz