When the last row in the table is selected,
removing another row may deselect the selected one
please follow this thread for more information:
http://forums.java.net/jive/thread.jspa?threadID=68361&tstart=0
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TableSortingBug {
private static void createGui() {
DefaultTableModel model = new DefaultTableModel(10, 1);
JTable table = new JTable(model);
// install a rowSorter
table.setAutoCreateRowSorter(true);
int last = table.getRowCount() - 1;
// select that last row
table.setRowSelectionInterval(last, last);
//sanity: really selected
if (!table.getSelectionModel().isSelectedIndex(last)) {
throw new RuntimeException();
}
// remove the second last
model.removeRow(last - 1);
if (table.getRowCount() - 1 != table.getSelectedRow()) {
throw new RuntimeException("last row must be still selected");
}
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
TableSortingBug.createGui();
}
});
}
}
Reproducible with the latest JDK 6 and 7