Reputation: 1
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 ?
Upvotes: 0
Views: 30
Reputation: 16137
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.
Upvotes: 3