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

JTable selection state corrupted after insertRow with RowFilter applied

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      When a JTable has a RowFilter applied and a new row is inserted at or before the currently selected row's model index, the selection state becomes corrupted, resulting in multiple rows being selected instead of maintaining the original single selection.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Selection should remain on a single row (the original selected data should stay selected)
      ACTUAL -
      Multiple rows become selected. Output shows selection changes from [2] to [1, 2], indicating two rows are now selected instead of one.

      ---------- BEGIN SOURCE ----------
      import java.util.*;
      import javax.swing.*;
      import javax.swing.table.*;

      public class TableTest {
          public static void main(String[] args) {
              DefaultTableModel model = new DefaultTableModel(0, 1);
              model.addRow(new Object[]{0});
              model.addRow(new Object[]{1});
              model.addRow(new Object[]{2});

              JTable table = new JTable(model);
              table.setAutoCreateRowSorter(true);
              table.setRowSelectionInterval(2, 2);
              System.out.println(Arrays.toString(table.getSelectedRows()));

              RowFilter<Object, Object> filter = new RowFilter<>() {
                  public boolean include(Entry<?, ?> entry) {
                      return (int) entry.getIdentifier() != 0;

                  }
              };
              ((DefaultRowSorter<?, ?>) table.getRowSorter()).setRowFilter(filter);
              model.insertRow(2, new Object[]{"x"});
              System.out.println(Arrays.toString(table.getSelectedRows()));

              if (table.getSelectedRowCount() != 1){
                  throw new RuntimeException("Test Failed: Selected row count is not 1 after inserting a new row.");
              }
          }
      }

      ---------- END SOURCE ----------

        1. TableTest.java
          1 kB
          Praveen Narayanaswamy

            tr Tejesh R
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: