Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving JTable line content after user sorted content by clicking on column

I have a pane with two tables A and B. When a row is selected in A, the content of B should be upated.

My code detects row selections in A fine. But, when the user clicks on the column header to sort rows, it does not seem like this is taken into account in A's table model.

So, I can get the selected row number (which is correct considering the sorting), but when I try to retrieve row field content from A using its table model, it gives me the values as if rows had not been sorted.

How can I retrieve the content of the selected line from the selected row number?

like image 342
Jérôme Verstrynge Avatar asked Dec 27 '22 18:12

Jérôme Verstrynge


1 Answers

Without any code, it is hard to say for sure what your problem is. However, it sounds like you are mixing up the row indices between the view and the model. You must be very clear about what co-ordinate system you are referring to (view or model) when you have a row number. See the JTable API for the convertRowIndexToModel and convertRowIndexToView methods.

You 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 );
like image 154
wolfcastle Avatar answered Feb 04 '23 15:02

wolfcastle