JTable and DefaultTableModel row indexes lose their sync after JTable sort

JAVA Netbeans

// resultsTable, myModel
JTable resultsTable;
DefaultTableModel myModel; //javax.swing.table.DefaultTableModel
myModel = (DefaultTableModel) resultsTable.getModel();

// event of clicking on item of table
String value = (String) myModel.getValueAt(resultsTable.getSelectedRow(), columnIndex)

I use JTable and DefaultTableModel to view a table of various information and I want to get the value of a specific column of the selected table index.

The code I wrote above works fine if: I use the GUI view (click on the name of the field I want to sort by table) The table is sorted correctly, but after that, when I select a row, it gets the value of the row that was there before sorting. This means that after sorting (using the JTable GUI) myModel and resultTable objects have different row indices.

How to sync these two?

+3
3

'convertXXX' JTable . JavaDoc

int row = resultsTable.getSelectedRow();
if (row != -1) {
   row = table.convertRowIndexToModel(row);
   String value = (String) myModel.getValueAt(row, columnIndex)
+10

, () :

// resultsTable, myModel

JTable resultsTable;

DefaultTableModel myModel; //javax.swing.table.DefaultTableModel

myModel = (DefaultTableModel) resultsTable.getModel();

// event of clicking on item of table

String value = (String) **resultsTable**.getValueAt(resultsTable.getSelectedRow(), columnIndex)

resultsTable myModel .

0

JTable.getValueAt() . , "" . AbstractTableModel.getValueAt() JTable.convertXXX() ( ), .

0

Source: https://habr.com/ru/post/1749900/


All Articles