static void setTableModel(final JTable table, final TableModel model) { swing { Map widths = tableColumnWidthsByName(table); int[] i = table.getSelectedRows(); RowSorter sorter = model.getColumnCount() == tableColumnCount(table) ? table.getRowSorter() : null; table.setModel(model); int n = model.getRowCount(); ListSelectionModel sel = table.getSelectionModel(); for (int j = 0; j < i.length; j++) if (i[j] < n) sel.addSelectionInterval(i[j], i[j]); tableSetColumnPreferredWidths(table, widths); if (sorter != null) print("Keeping sorter: " + sorter); table.setRowSorter(sorter); } }