4

I have a JTable to which I linked a DefaultTableModel and a TableRowSorter. They work. When you click in a row, its content is copied to JTextfields so you can edit them*. But if you sort the table by some field and then click in a row, the copied content is taken from its previous value (before the sort) instead from the new one. What can be causing this?

*I do this because the table only contains a subset of all fields.

2
  • 2
    Without any code, we can only assume what's wrong with it. My hunch is that you missed the link between model and table. after sorting only the view is changed, model remains the same. so the selection index is not same as the values at the index in the model. Check TableRowSorter usage, especially convertRowIndexToModel method in the table.
    – Manoj
    May 18, 2011 at 11:08
  • That was exactly it, you guys have such good code noses!
    – deprecated
    May 18, 2011 at 11:20

1 Answer 1

7

Looks like when you are getting the selected row index you are not converting it to the model index before you grab the data.

Instead of this:

table.getSelectedRow()

Use this:

table.convertRowIndexToModel(table.getSelectedRow());
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.