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

JTable repaints incorrectly whena RowSorter with setSortsOnUpdate as true is set

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]
      SunOS 5.10 SunOS_Development i86pc i386 i86pc


      A DESCRIPTION OF THE PROBLEM :
      Set RowSorter on a JTable and do setSortsOnUpdate(true). If a table update displaces a row to a new location, the old row is not invalidated and repainted always.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached program. Once the JFrame appears, watch it (do nothing to force a repaint). The table is sorted on Symbol column. So we see AMZN and GOOG in ascending order.

      One of the rows with 'AMZN' is updated to 'YHOO'. As setSortsOnUpdate is true, this row moves to index 1. And GOOG moves to index 0. However, after the update you end up both rows with GOOG in column 0 until you force a repaint by resizing the dialog or clicking on it.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Both the cells tha are displaced should be repainted.
      ACTUAL -
      In this case, only one of them is repainted until I forced a repaint by resize or clicks.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.DefaultRowSorter;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.SortOrder;
      import javax.swing.SwingWorker;
      import javax.swing.RowSorter.SortKey;
      import javax.swing.table.DefaultTableModel;
      import javax.swing.table.TableModel;
      import javax.swing.table.TableRowSorter;

      import java.awt.BorderLayout;
      import java.awt.EventQueue;
      import java.util.ArrayList;
      import java.util.List;

      /**
       * Program to indicate Bug in JTable's repainting of rows. The old row
       * is not repainted if setSortsOnUpdate is true.
       */
      public class BugSort
      {
          /**
           * @param args none
           */
          public static void main(String args[]) {
              Runnable runner = new Runnable() {
                  @SuppressWarnings("unchecked")
                  public void run()
                  {
                      JFrame frame = new JFrame
                          ("Bug in RowSorter.setSortsOnUpdate JTable");
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      Object rows[][] = {
                              {"AMZN", "sometext", 41.28},
                              {"GOOG", "moretext", 388.33},
                      };
                      String columns[] = {"Symbol", "Name", "Price"};
                      TableModel model =
                          new DefaultTableModel(rows, columns) {
                          public Class getColumnClass(int column) {
                              Class returnValue;
                              if ((column >= 0) && (column < getColumnCount())) {
                                  returnValue = getValueAt(0, column).getClass();
                              } else {
                                  returnValue = Object.class;
                              }
                              return returnValue;
                          }
                      };

                      final JTable table = new JTable(model);
                      final TableRowSorter<TableModel> sorter =
                          new TableRowSorter<TableModel>(model);
                      table.setRowSorter(sorter);
                      List<SortKey> keys = new ArrayList<SortKey>();
                      keys.add(0, new SortKey(0, SortOrder.ASCENDING));
                      table.getRowSorter().setSortKeys(keys);
                      ((DefaultRowSorter) table.getRowSorter()).setSortsOnUpdates(true);
                      JScrollPane pane = new JScrollPane(table);
                      frame.add(pane, BorderLayout.CENTER);
                      JPanel panel = new JPanel(new BorderLayout());
                      frame.add(panel, BorderLayout.NORTH);
                      frame.setSize(300, 250);
                      frame.setVisible(true);


                      SwingWorker worker = new SwingWorker() {
                          @Override
                          protected Object doInBackground() throws Exception
                          {
                              // Sleep a bit so that we can look at the table
                              // to notice the ordering.
                              Thread.sleep(5000);
                              return null;
                          }
                          public void done() {
                              System.out.println("setting YHOO AT 0, 0");
                              // This moves the row at 0,0 to 1,0
                              table.setValueAt("YHOO",0,0);
                          }
                      };
                      worker.execute();
                  }
              };
              EventQueue.invokeLater(runner);
          }
      }

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

            peterz Peter Zhelezniakov
            ryeung Roger Yeung (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: