strange behavior of JTable..Help plz

Hi,

Assume i have 5 rows in my JTable. I wrote a function on right click i get a popup window with an option delete row, which deletes the row successfully,

The problem is when i sort the data in JTable. on column click JTable is getting sorted, in which assume 2nd row got replaced with 4th row and vice versa.

Now when i try to delete the 4th row, it is delete the 2nd row data, which was there at the place of 4th row before sorting. In this regard i am trying to print the row number before deleting. It is displaying as 4th, but deleting 2nd row.

Hope i am clear. Please suggest me in solving this strange behavior of JTable sorting problem.

Regards,

Ravi

[703 byte] By [rkrgarlapatia] at [2007-11-27 4:54:15]
# 1
The problem is the tableSorterModel i guess ...This model saves a reference of the current table model to store the indice of each row. so the index of a row in the sorterModel is different from the current tabel model. You need to use the indice of the sorterModel to delete a row.
Oleka at 2007-7-12 10:08:51 > top of Java-index,Desktop,Core GUI APIs...
# 2

for sorting the table data i am using the below java code as is, from 'How to Use JTable' site.

http://java.sun.com/docs/books/tutorial/uiswing/examples/components/TableSorterDemoProject/src/components/TableSorter.java

Please suggest whether to make any changes in this code.

rkrgarlapatia at 2007-7-12 10:08:51 > top of Java-index,Desktop,Core GUI APIs...
# 3
Look at the TableSorter class. There is a method that will convert the view row to the model row so that you can delete the correct row from the model.
camickra at 2007-7-12 10:08:51 > top of Java-index,Desktop,Core GUI APIs...
# 4

MyTableModel tablemodel = new MyTableModel(colnames,values);

TableSorter sorter = new TableSorter(tablemodel);

JTable table = new JTable(sorter)

tablemodel.removeRow(row);

// last line is the code where i am deleting the row, it's deleting the wrong row.

do i need to remove the last line code and make respective changes in TableSorter.java class.

ore

here itself do i need to do some changes. ?

>There is a method that will convert the view row to the model row

private Row[] getViewToModel()

my question is how TableSorter.java class comes to know that i am deleting the perticular row.

rkrsujathaa at 2007-7-12 10:08:51 > top of Java-index,Desktop,Core GUI APIs...
# 5
TableSorter has a public function called modelIndex that will convert the index of your row in view to the index in the model.So just use this function like this:tablemodel.removeRow(sorter.modelIndex(row));
Rodney_McKaya at 2007-7-12 10:08:51 > top of Java-index,Desktop,Core GUI APIs...
# 6
u r magnificent !GOT IT......thx a TON.
rkrgarlapatia at 2007-7-12 10:08:51 > top of Java-index,Desktop,Core GUI APIs...