JTable's and DefaultTableModel's row indexes lose their synchronization after I sort JTable

Posted by Stefanos Kargas on Stack Overflow See other posts from Stack Overflow or by Stefanos Kargas
Published on 2010-06-14T09:29:19Z Indexed on 2010/06/14 9:32 UTC
Read the original article Hit count: 474

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 info and I want to get a value of a certain column of the selected index of the table.

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

How do I synchronize those two?

© Stack Overflow or respective owner

Related posts about java

Related posts about sorting