-1

I have JTable object, filled with data provided from my implementation of AbstractTableModel. I have mouse event listener and when I click on some cell I get its row and column position. With this values I call getValueAt(int row, int column) method from TableModel and I get my data. The problem is the data in the table can be sorted when I click on column name. When again I try to call getValueAt() I get the old value Example:

col1|col2

val11|val21

val12|val22 When I click on col1

values are:

col1|col2

val12|val22

val11|val21

The I call getValueAt(0,0) and still get val11, and I should get val12. How can I fix this ? Thanks!

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3546762
  • 117
  • 1
  • 2
  • 6

2 Answers2

3

The row index in the view (that you probably get from your mouse listener - if you posted the code as requested, we would know instead of having to guess) is not the same as the row index in the model, once the table is sorted. To convert from the view index to the model index, use convertRowIndexToModel().

Note that the same care must be taken for columns, as the user can reorder them by drag and dropping them.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

I've manage to do it. table.getValueAt() should be called direct to the table object. My previous attempt was table.getModel().getValueAt().

user3546762
  • 117
  • 1
  • 2
  • 6