开发者

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

开发者 https://www.devze.com 2023-03-14 01:22 出处:网络
I have a pane with two tables A and B. When a row is selected in A, the content of B should be upated.

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 t开发者_JAVA百科he selected row number?


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 );
0

精彩评论

暂无评论...
验证码 换一张
取 消