1

I have the following jtable which is sorted on sortKey column in ascending order.

row# col1 col2 col3 sortKey
1     a    b    c    1
2     d    e    f    2
3     g    h    i    3

I have used the TableRowSorter to sort this JTable. now i want to insert an empty row between row#1 and row#2 the result should look like.

row# col1 col2 col3 sortKey
1     a    b    c    1
2
3     d    e    f    2
4     g    h    i    3

but instead i get the result like

row# col1 col2 col3 sortKey
1
2     a    b    c    1
3     d    e    f    2
4     g    h    i    3

how can i fix the position of empty row into this table when it is sorted?

Nadeem
  • 195
  • 1
  • 1
  • 14
  • duplicate of this one: http://stackoverflow.com/questions/16970675/how-do-i-add-an-empty-row-to-jtable – blipman17 Jan 08 '15 at 10:31
  • 1
    @blipman17: And what things do you think that are duplicate to this question? these are completely different questions man...there may be hundreds of questions regarding adding an empty row to jtable but tell me how to handle with sorted table. – Nadeem Jan 08 '15 at 10:36
  • If you have your own `TableRowSorter`, just make sure it uses the correct position for the empty row – Robin Jan 08 '15 at 13:38

1 Answers1

2

If you have a JTable with a RowSorter and add/insert a new row, the row is shown at the position determined by the RowSorter. So if in your example an empty row (thus with null values) is inserted, the row is shown at first position because "null" is smaller than "1".

If you want to show it at a different position you have 2 Options:

  1. You provide an appropriate value for the column sortKey (on inserting the row). If you have like in your example integer values as sort keys and you want to show a row between the rows with sort keys "1" and "2" you have to give the new row the value "2" and adapt the sort key values of all following rows.

  2. You deactivate the sorting by calling table.setRowSorter (null). Keep in mind to reactivate the sorting after filling the row with values by calling table.setRowSorter again with your row sorter.

General Martok
  • 175
  • 1
  • 6