JTable has an auto sorting feature through the use of the setAutoCreateRowSorter() method. However, try inserting a new row to its DefaultTableModel and you will find out it is not displayed correctly at the right index row position if the JTable is in sort mode.

There is no automatic way to fix this problem with just a set method. You will have to instantiate a TableRowSorter object and assign that DefaultTableModel as its parameter, assign it to the JTable‘s setRowSorter() method and set the TableRowSorter‘s setSortsOnUpdates() method to true.

See sample code below.

That should fix it. So whenever you insert a new row and you already clicked a column header to sort the JTable in ascending or descending order, the newly added row will be sorted accordingly right away.

Related Posts Plugin for WordPress, Blogger...