Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6894632

Removing rows from a DefaultTableModel with a RowSorter deselectes last row

XMLWordPrintable

    • b61
    • generic
    • generic

        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

              ssadetsky Semyon Sadetsky (Inactive)
              alexp Alexander Potochkin (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: