Here is how my data in jTable looks:
SrNo Name LName
1 A B
2 B C
3 D E
4 F G
now when I click row 4, and click a button "Move to TOP", the table should look like this,
SrNo Name LName
1 F G
2 A B
3 B C
4 D E
I could think of two approaches:
- Swap(just content of Name and LName) row 4 with ro开发者_运维问答w 3, row 3 with row 2, do this till we reach to top.
- Change the SrNo of the row selected to 1, increase SrNo of all the rows above it, decrease the SrNo of all the rows below it and sort by SrNo.
Which is good approach? Or is there any better approach?
(This table is binded to database, but it is another problem)
The DefaultTableModel already supports a moveRow(...) method.
Also, I have no idea what the SrNo is, but if the value is just the row number then it should not be part of the model. Instead is should be rendered as part of the row header view. Maybe something like the Row Number Table.
Use a table model, which is backed up by a list of POJOs such as this:
http://puces-samples.svn.sourceforge.net/viewvc/puces-samples/trunk/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java?revision=2&view=markup
Add a method like this:
public void moveToTop(int index){
MyPojoClas myPojo = myList.remove(index);
myList.add(0, myPojo);
fireTableRowsUpdated(0, index);
}
精彩评论