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

JTable: selection state broken after insert row

XMLWordPrintable

    • generic
    • generic

      FULL PRODUCT VERSION :
      C:\Users\kleopatra>java -version
      java version "1.6.0_12"
      Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
      Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      doesn't matter

      A DESCRIPTION OF THE PROBLEM :
      see java doc of example and discussion in the swinglabs forum:

      http://forums.java.net/jive/thread.jspa?threadID=57600&tstart=0

      (don't know if it's a regression in the update-releases of 1.6, obviously couldn't have happened earlier than that)

      Regards
      Jeanette

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      see java doc of example

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      see java doc of example
      ACTUAL -
      see java doc of example

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * Created on 18.02.2009
       *
       */
      package org.jdesktop.swingx.table;

      import java.awt.BorderLayout;
      import java.awt.event.ActionEvent;

      import javax.swing.AbstractAction;
      import javax.swing.Action;
      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.RowFilter;
      import javax.swing.table.DefaultTableModel;
      import javax.swing.table.TableRowSorter;

      /**
       * Selection behaviour on insert row at top of model is broken.
       *
       * At start: first row selected, then add rows at top
       *
       * Two (at least, didn't dig deeper ;-)
       *
       * A) inconsistent for with/-out RowSorter -- with: inserted row is not selected
       * -- without: inserted row is selected (while this is a bug morphed into a
       * "feature" ages ago, that's the pre-1.6 behaviour)
       *
       * To reproduce, run the example as-is/commented autoCreateSorter for
       * with/without and press insert button
       *
       *
       * B) selection state is "lost" once a RowFilter had been set
       *
       * To reproduce, run the example as-is and - insert some rows: the selection is
       * kept on the initially selected row (in model coordinates) That's correct
       * behaviour. - toggle the filter on and off again - insert some rows: the
       * selection is kept fixed on the _view_ selection, that's wrong, should behave
       * the same way as before having toggled the filter
       */
      public class SelectionIssue {
          public static void main(String[] args) {
              final DefaultTableModel model = new DefaultTableModel(new String[][] {
                      { "row0" }, { "row1" } }, new String[] { "col" });
              final JTable table = new JTable();
              // comment this line to get old-selection-extension
              table.setAutoCreateRowSorter(true);
              table.setModel(model);
              table.setRowSelectionInterval(0, 0);

              Action insertRow = new AbstractAction("Insert Row At Top") {

                  public void actionPerformed(ActionEvent evt) {
                      String text = "row-" + ((model.getRowCount() - 2));
                      model.insertRow(0, new String[] { text });
                  }
              };

              final RowFilter filter = RowFilter.regexFilter("0$");
              Action toggleFilter = new AbstractAction("toggle filter") {

                  @Override
                  public void actionPerformed(ActionEvent e) {
                      TableRowSorter sorter = (TableRowSorter) table.getRowSorter();
                      RowFilter old = sorter.getRowFilter();
                      sorter.setRowFilter(old != null ? null : filter);

                  }

              };
              toggleFilter.setEnabled(table.getRowSorter() != null);
              JFrame frame = new JFrame("Selection state broken");
              frame.add(new JScrollPane(table));
              frame.add(new JButton(insertRow), BorderLayout.SOUTH);
              frame.add(new JButton(toggleFilter), BorderLayout.NORTH);
              frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              frame.setSize(400, 400);
              frame.setVisible(true);
          }

      }

            pbansal Pankaj Bansal (Inactive)
            alexp Alexander Potochkin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: