I have a JTable filled with data of a table of my database (so I used ResultSetTableModel) and u开发者_如何转开发sing TableRowSorter to sort the rows, as I click in one column of the JTable. The data is displayed in the jTable without problems;
But when I sort the JTable by some column table (for example, sorting it by the primary key value), and edit some cell from the sorted jTable, the value changed is the old cell that were in that position before the ordenation of the column.
For example: Suppose I have a table with 2 columns - name and age. My table has the following data:
c 1
b 2
a 3
when I order i by name in the JTable, it becomes like this
a 3
b 2
c 1
if I edit the value "1", after the edition, the table becomes like this
a 1
b 2
c 1
It seems that the positions are not being updated in the JTable, and the values are edited considering their original positions.
Firstly, note that data is actually contained in the model and jtable is just a view. Normally, by default the jtable's rows and columns correspond to rows and columns of the table. When you sort, this mapping between jtable's row,col may not remain the same, so when you want to edit say cell(rowVal,colVal), do
table.setValueAt(object,table.convertRowIndexToModel(rowVal),convertColumnIndexToModel(colVal))
this should preserve the mapping after sorting. Read the section Sorting and Filtering here: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
精彩评论