开发者

Java: JTable reordering rows and refresh

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 app开发者_运维技巧roaches 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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜