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

TableRowSorter: sorting with comparator usage fail in some cases

    XMLWordPrintable

    Details

    • Subcomponent:
    • Introduced In Build:
      b56
    • Introduced In Version:
      6
    • Resolved In Build:
      b62
    • CPU:
      generic
    • OS:
      windows_xp

      Description

      The following code works normally on b55, but on b56 exception is throwing.

      ====== Code ======
      import javax.swing.RowSorter;
      import javax.swing.SortOrder;
      import javax.swing.table.TableModel;
      import javax.swing.table.DefaultTableModel;
      import javax.swing.table.TableRowSorter;
      import java.util.Comparator;
      import java.util.LinkedList;

      public class TestBug {
          private Integer[][] modelData = {
                  {0, 1, 2},
                  {2, 0, 1}
          };
          private Integer[] columnNames = {1, 2, 3};

          class TestComparator implements Comparator<Integer> {
              public int compare(Integer i1, Integer i2) {
                  if (i1 == 0) i1 = 100;
                  if (i2 == 0) i2 = 100;
                  return i1 - i2;
              }
          }

          private void test() {
              TableModel model = new DefaultTableModel(modelData, columnNames);
              LinkedList<RowSorter.SortKey> keys =
                      new LinkedList<RowSorter.SortKey>();
              TableRowSorter<TableModel> rs = new TableRowSorter<TableModel>(model);
              // at the first time everything is ok
              rs.setComparator(1, new TestComparator());
              keys.add(new RowSorter.SortKey(1, SortOrder.ASCENDING));
              System.out.println("Check point");
              rs.setSortKeys(keys);
              // at the second time:
              // java.lang.ClassCastException: java.lang.String
              // cannot be cast to java.lang.Integer
              keys.clear();
              rs.setComparator(2, new TestComparator());
              keys.add(new RowSorter.SortKey(2, SortOrder.ASCENDING));
              rs.setSortKeys(keys);
          }

          public static void main(String[] args) {
              TestBug t = new TestBug();
              t.test();
          }
      }
      ======= Output ========
      Check point
      Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
      at TableRowSorter.TestBug$TestComparator.compare(TestBug.java:19)
      at javax.swing.DefaultRowSorter.compare(DefaultRowSorter.java:869)
      at javax.swing.DefaultRowSorter.access$100(DefaultRowSorter.java:91)
      at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1277)
      at javax.swing.DefaultRowSorter$Row.compareTo(DefaultRowSorter.java:1267)
      at java.util.Arrays.mergeSort(Arrays.java:1147)
      at java.util.Arrays.sort(Arrays.java:1082)
      at javax.swing.DefaultRowSorter.sortExistingData(DefaultRowSorter.java:477)
      at javax.swing.DefaultRowSorter.setSortKeys(DefaultRowSorter.java:251)
      at TableRowSorter.TestBug.test(TestBug.java:40)
      at TableRowSorter.TestBug.main(TestBug.java:48)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:589)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86)

      Process finished with exit code 1

        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: