Retrieving the contents of a JTable row after sorting a user by clicking on a column

I have a panel with two tables A and B. When the row is selected in A, the contents of B should be enlarged.

My code defines row selection in fine. But, when the user clicks on the column heading to sort the rows, it seems that this is not taken into account in the table model.

So, I can get the selected row number (which is correct, given sorting), but when I try to get the contents of the row field from A using my table model, it gives me the values ​​as if the rows were not sorted.

How to get the contents of a selected row from a selected row number?

+4
source share
1 answer

It’s hard to say what the problem is without any code. However, it looks like you are mixing row indices between the view and the model. You must clearly understand which coordinate system you have in mind (view or model) when you have a line number. See the JTable API for the convertRowIndexToModel and convertRowIndexToView .

You will probably need something like this:

 JTable table = ...; TableModel model = ...; int viewRow = table.getSelectedRow(); int modelRow = table.convertRowIndexToModel(viewRow); int viewColumn = table.getSelectedColumn(); int modelColumn = table.convertColumnIndexToModel(viewColumn); Object cell = model.getValueAt( modelRow, modelColumn ); 
+10
source

All Articles