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

JTable sorter fires even if the model is empty

    XMLWordPrintable

    Details

      Description

      FULL PRODUCT VERSION :
      java version "1.6.0_20"
      Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
      Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Professional

      A DESCRIPTION OF THE PROBLEM :
      When the DefaultTableModel of a sortable JTable is cleared by means of a
      tblModel.setRowCount(0);
      the sorter gets fired, which leads to an
      java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the example code, click on any column's header, then press [Update]

      Note: As long as you do NOT press a header, the code works fine.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      No ArrayIndexOutOfBounds (or any other) Exception
      ACTUAL -
      ArrayIndexOutOfBoundsException: 0 >= 0

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:
       0 >= 0
              at java.util.Vector.elementAt(Unknown Source)
              at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
              at UpdateSortableTable$1.getColumnClass(UpdateSortableTable.java:52)
              at javax.swing.table.TableRowSorter.useToString(Unknown Source)
              at javax.swing.DefaultRowSorter.updateUseToString(Unknown Source)
              at javax.swing.DefaultRowSorter.sort(Unknown Source)
              at javax.swing.DefaultRowSorter.shouldOptimizeChange(Unknown Source)
              at javax.swing.DefaultRowSorter.rowsDeleted(Unknown Source)
              at javax.swing.JTable.notifySorter(Unknown Source)
              at javax.swing.JTable.sortedTableChanged(Unknown Source)
              at javax.swing.JTable.tableChanged(Unknown Source)
              at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)

              at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(Unknown Sou
      rce)
              at javax.swing.table.DefaultTableModel.setNumRows(Unknown Source)
              at javax.swing.table.DefaultTableModel.setRowCount(Unknown Source)
              at UpdateSortableTable$2.actionPerformed(UpdateSortableTable.java:79)
              at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
              at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
              at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
              at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
              at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
      ce)
              at java.awt.Component.processMouseEvent(Unknown Source)
              at javax.swing.JComponent.processMouseEvent(Unknown Source)
              at java.awt.Component.processEvent(Unknown Source)
              at java.awt.Container.processEvent(Unknown Source)
              at java.awt.Component.dispatchEventImpl(Unknown Source)
              at java.awt.Container.dispatchEventImpl(Unknown Source)
              at java.awt.Component.dispatchEvent(Unknown Source)
              at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
              at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
              at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
              at java.awt.Container.dispatchEventImpl(Unknown Source)
              at java.awt.Window.dispatchEventImpl(Unknown Source)
              at java.awt.Component.dispatchEvent(Unknown Source)
              at java.awt.EventQueue.dispatchEvent(Unknown Source)
              at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
              at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
              at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
              at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
              at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
              at java.awt.EventDispatchThread.run(Unknown Source)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.table.*;

      public class UpdateSortableTable extends JFrame {
        final String HEADER[]= {"No.", "Item", "Price", "Count"};
        Object data[][]= {
      {1, "Oak", 7.40, 1000},
      {2, "Beech", 10.50, 500},
      {3, "Cherry", 15.00, 150},
      {4, "Pine", 5.0, 2000},
      };
        DefaultTableModel tblModel;
        JTable table;


        public UpdateSortableTable() {
          super("UpdateSortableTable");
          setSize(315, 150);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Container contentPane = getContentPane();

          tblModel = new DefaultTableModel(data, HEADER) {
            public Class getColumnClass(int column) {
      Class returnValue;
      if (column>=0 && column<getColumnCount()) {
      returnValue= getValueAt(0, column).getClass();
      }
      else {
      returnValue= null;//Object.class;
      }
      return returnValue;
            }

      // Make read-only
            public boolean isCellEditable(int x, int y) {
      return false;
            }
          };
          table = new JTable(tblModel);
          table.setAutoCreateColumnsFromModel(false);
          table.setAutoCreateRowSorter(true);

          JScrollPane scrollPane = new JScrollPane (table);
          contentPane.add(scrollPane, BorderLayout.CENTER);
          JButton bUpdate= new JButton("Update");
          bUpdate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
      data[0][1]= "Maple";
      tblModel.setRowCount(0);
      tblModel.addRow(data[0]);
      tblModel.addRow(data[1]);
            }
          });
          contentPane.add(bUpdate, BorderLayout.SOUTH);
          setVisible(true);
        }

        public static void main(String args[]) {
          EventQueue.invokeLater(new Runnable() {
            public void run() {
      new UpdateSortableTable();
            }
          });
        }

      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Either (1.)
      {code}
      /* The sorter obviously gets fired even if there are no rows. So check the
      rowCount in
      public Class getColumnClass(int column)
      */
      if (getRowCount()>0) {
      returnValue= getValueAt(0, column).getClass();
      }
      else {
      returnValue= null;
      }
      return returnValue;
      {/code}


      or (2.)
      Clear the tableModel in a loop
      {code}
      int j= tblModel.getRowCount();
      for (int i=0; i<j; i++)
      tblModel.removeRow(0);
      {/code}

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                vdyakov Victor Dyakov
                Reporter:
                igor Igor Nekrestyanov (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Imported:
                  Indexed: