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

javax.swing.JTable.setRowSorter(RowSorter) method doesn't clear selection in some cases

    XMLWordPrintable

    Details

      Description

      In specification is written:

      public void setRowSorter(RowSorter sorter)

          Sets the RowSorter. RowSorter is used to provide sorting and filtering to a JTable.

          This method *clears the selection* and resets any variable row heights.

          If the underlying model of the RowSorter differs from that of this JTable undefined behavior will result.

          Parameters:
              sorter - the RowSorter; null turns sorting off

      But when I run the folloving code:

      import javax.swing.table.DefaultTableModel;
      import javax.swing.table.TableRowSorter;
      import javax.swing.table.TableModel;
      import javax.swing.JTable;
      import javax.swing.RowSorter;
      import java.util.Arrays;

      public class Test1 {

      public static void main(String argv[]) {
      Test1 test = new Test1();
      test.setGetRowSorterTest();
      }

      private int[][] rowsToSelect =
      {
      {0, 2, 4},
      {0},
      {0, 1, 2, 3, 4},
      {3, 4}
      };

      private void selectRows(int[] rowsToSelect, JTable table) {
      table.setColumnSelectionAllowed(false);
      table.changeSelection(rowsToSelect[0], 0, false, false);
      for (int i = 1; i < rowsToSelect.length; i++) {
      table.changeSelection(rowsToSelect[i], 0, true, false);
      }
      }

      private void prinSelectedRows(JTable table, String ms) {
      System.out.println(ms + ": table.getSelectedRows() = " + Arrays.toString(table.getSelectedRows()));
      }

      public void setGetRowSorterTest() {
      TableModel model = new DefaultTableModel(5, 3);
      JTable table = new JTable(model);
      RowSorter[] rowSorters = {
      new TableRowSorter(model),
      null,
      new TableRowSorter(),
      };
      for (int i = 0; i < rowSorters.length; i++) {
      for (int[] selectedRows : rowsToSelect) {

      selectRows(selectedRows, table);
      System.out.println("rowsToSelect = " +
      Arrays.toString(selectedRows) +
      "; Sorter = " + rowSorters[i]);
      prinSelectedRows(table, "BEFORE");
      table.setRowSorter(rowSorters[i]);
      prinSelectedRows(table, "AFTER");
      }
      }
      }
      }
      -----------------------------
       I get the output:
      -----------------------------
      rowsToSelect = [0, 2, 4]; Sorter = javax.swing.table.TableRowSorter@901887
      BEFORE: table.getSelectedRows() = [0, 2, 4]
      AFTER: table.getSelectedRows() = [1, 2, 3, 4]
      rowsToSelect = [0]; Sorter = javax.swing.table.TableRowSorter@901887
      BEFORE: table.getSelectedRows() = [0]
      AFTER: table.getSelectedRows() = []
      rowsToSelect = [0, 1, 2, 3, 4]; Sorter = javax.swing.table.TableRowSorter@901887
      BEFORE: table.getSelectedRows() = [0, 1, 2, 3, 4]
      AFTER: table.getSelectedRows() = [1, 2, 3, 4]
      rowsToSelect = [3, 4]; Sorter = javax.swing.table.TableRowSorter@901887
      BEFORE: table.getSelectedRows() = [3, 4]
      AFTER: table.getSelectedRows() = [1, 2, 3, 4]
      rowsToSelect = [0, 2, 4]; Sorter = null
      BEFORE: table.getSelectedRows() = [0, 2, 4]
      AFTER: table.getSelectedRows() = [1, 2, 3, 4]
      rowsToSelect = [0]; Sorter = null
      BEFORE: table.getSelectedRows() = [0]
      AFTER: table.getSelectedRows() = []
      rowsToSelect = [0, 1, 2, 3, 4]; Sorter = null
      BEFORE: table.getSelectedRows() = [0, 1, 2, 3, 4]
      AFTER: table.getSelectedRows() = [1, 2, 3, 4]
      rowsToSelect = [3, 4]; Sorter = null
      BEFORE: table.getSelectedRows() = [3, 4]
      AFTER: table.getSelectedRows() = [1, 2, 3, 4]
      rowsToSelect = [0, 2, 4]; Sorter = javax.swing.table.TableRowSorter@3a6727
      BEFORE: table.getSelectedRows() = [0, 2, 4]
      AFTER: table.getSelectedRows() = []
      rowsToSelect = [0]; Sorter = javax.swing.table.TableRowSorter@3a6727
      BEFORE: table.getSelectedRows() = [0]
      AFTER: table.getSelectedRows() = []
      rowsToSelect = [0, 1, 2, 3, 4]; Sorter = javax.swing.table.TableRowSorter@3a6727
      BEFORE: table.getSelectedRows() = [0, 1, 2, 3, 4]
      AFTER: table.getSelectedRows() = []
      rowsToSelect = [3, 4]; Sorter = javax.swing.table.TableRowSorter@3a6727
      BEFORE: table.getSelectedRows() = [3, 4]
      AFTER: table.getSelectedRows() = []
      ---------------------------------------------------------

      So, in cases when RowSorter = null, new TableRowSorter(model) the selection is not cleared.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              svioletsunw Scott Violet (Inactive)
              Reporter:
              ynovozhi Yulia Novozhilova (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: