简体   繁体   中英

JTable sorting not the good value in the cell

I am using NetBeans to write a program. I create a JTable with the design and I add rows to the table without any problem.

When I click on the tab to sort the table it works, with setAutoCreateRowSorter(true) . However when I try to get the value of the updated cell it is not the good one. Indeed, it is the value of the previous cell, when I fill my table.

This is how I add row to my table:

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
for (int i = 0; i < arrayPep.size(); i++) {
   model.addRow(new Object[]{arrayPep.get(i).getPeptide(), arrayPep.get(i).getStart(), arrayPep.get(i).getStop(), arrayPep.get(i).geteValue(), arrayPep.get(i).getName()});
}

This is how I try to get the value of the cell:

DefaultTableModel model = (DefaultTableModel) table.getModel();
table.getModel().getValueAt(rowSelected, 0);

How could I fix this ?

You are indexing the model with an index from the view. First convert the view index to a model index using JTable.convertRowIndexToModel(rowSelected) :

int modelRowSelected = table.convertRowIndexToModel(rowSelected);
table.getModel().getValueAt(modelRowSelected, 0);

For some context regarding view indexes and model indexes, please read this answer I gave on the topic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM