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

Specification for the javax.swing.DefaultRowSorter.setSortsOnUpdates/rowsUpdated method is not clear

XMLWordPrintable

    • b48
    • generic
    • windows_xp

      In spec for javax.swing.DefaultRowSorter.setSortsOnUpdates method is written:
      ---------------------------------
      public void setSortsOnUpdates(boolean sortsOnUpdates)

          If true, specifies that a sort should happen when the underlying model is updated (rowsUpdated is invoked). For example, if this is true and the user edits an entry the location of that item in the view may change. The default is false.
      -----------------------------------

      public void rowsUpdated(int firstRow,
                              int endRow)

          Invoked when rows have been changed in the underlying model between the specified range (inclusive).

          You normally do not call this method. This method is public to allow view classes to call it.
      --------------------------------------

      I'm interested why a sort *doesn't happen* when I run the following code?
      ========================================================================
      import javax.swing.table.TableModel;
      import javax.swing.table.TableRowSorter;
      import javax.swing.table.DefaultTableModel;
      import javax.swing.*;
      import java.util.LinkedList;

      public class TestSetSortsOnUpdates {
      public static void main(String[] args) {
      TestSetSortsOnUpdates test = new TestSetSortsOnUpdates();
      System.out.println("isSorted = " + test.test()));
      }
      class TestTableModel extends DefaultTableModel {
      public TestTableModel(int rows, int columns) {
      super(rows, columns);
      for (int i = 0; i < rows; i++) {
      for (int j = 0; j < columns; j++) {
      setValueAt(i * j, i, j);
      }
      }
      }

      public Class<?> getColumnClass(int columnx) {
      return Integer.class;
      }
      }


      private boolean isSorted(TableRowSorter rs, int[] expectedIndices) {
      boolean isSorted = true;
      for (int i = 0; i < rs.getTableModel().getRowCount(); i++) {
      if (rs.convertRowIndexToModel(i) != expectedIndices[i]) {
      isSorted = false;
      }
      }
      return isSorted;
      }


      public boolean test() {
      TableModel dataModel = new TestTableModel(3,3);
      TableRowSorter rs = new TableRowSorter(dataModel);
      LinkedList<RowSorter.SortKey> keys = new LinkedList<RowSorter.SortKey>();
      keys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
      rs.setSortKeys(keys);
      rs.setSortsOnUpdates(true);
      rs.getTableModel().setValueAt(1, 0, 0);
      //-----------MODEL----
      // 1 0 0
      // 0 1 2
      // 0 2 4
      //---------------------
      int[] expectedIndices = {
      1, 2, 0
      };
      return isSorted(rs, expectedIndices);
      }
      }

      ==================Tets Output===============
      isSorted = false
      In spec for javax.swing.DefaultRowSorter.setSortsOnUpdates method is written:
      ---------------------------------
      public void setSortsOnUpdates(boolean sortsOnUpdates)

          If true, specifies that a sort should happen when the underlying model is updated (rowsUpdated is invoked). For example, if this is true and the user edits an entry the location of that item in the view may change. The default is false.
      -----------------------------------

      public void rowsUpdated(int firstRow,
                              int endRow)

          Invoked when rows have been changed in the underlying model between the specified range (inclusive).

          You normally do not call this method. This method is public to allow view classes to call it.
      --------------------------------------

      I'm interested why a sort *doesn't happen* when I run the following code?
      ========================================================================
      import javax.swing.table.TableModel;
      import javax.swing.table.TableRowSorter;
      import javax.swing.table.DefaultTableModel;
      import javax.swing.*;
      import java.util.LinkedList;

      public class TestSetSortsOnUpdates {
      public static void main(String[] args) {
      TestSetSortsOnUpdates test = new TestSetSortsOnUpdates();
      System.out.println("isSorted = " + test.test()));
      }
      class TestTableModel extends DefaultTableModel {
      public TestTableModel(int rows, int columns) {
      super(rows, columns);
      for (int i = 0; i < rows; i++) {
      for (int j = 0; j < columns; j++) {
      setValueAt(i * j, i, j);
      }
      }
      }

      public Class<?> getColumnClass(int columnx) {
      return Integer.class;
      }
      }


      private boolean isSorted(TableRowSorter rs, int[] expectedIndices) {
      boolean isSorted = true;
      for (int i = 0; i < rs.getTableModel().getRowCount(); i++) {
      if (rs.convertRowIndexToModel(i) != expectedIndices[i]) {
      isSorted = false;
      }
      }
      return isSorted;
      }


      public boolean test() {
      TableModel dataModel = new TestTableModel(3,3);
      TableRowSorter rs = new TableRowSorter(dataModel);
      LinkedList<RowSorter.SortKey> keys = new LinkedList<RowSorter.SortKey>();
      keys.add(new RowSorter.SortKey(0, SortOrder.ASCENDING));
      rs.setSortKeys(keys);
      rs.setSortsOnUpdates(true);
      rs.getTableModel().setValueAt(1, 0, 0);
      //-----------MODEL----
      // 1 0 0
      // 0 1 2
      // 0 2 4
      //---------------------
      int[] expectedIndices = {
      1, 2, 0
      };
      return isSorted(rs, expectedIndices);
      }
      }

      ==================Tets Output===============
      isSorted = false
      ============================================

      Actually, the problem concerns the methods rowsInserted/rowsDeleted/rowsUpdated.
      These methods are invoked from the JTable class only. But TableRowSorter also has the underlying model. That is why the specification for these methods seems to be unclear .

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: