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

REGRESSION: 5.0 can throw a RTE if a ListSelectionListener is used and setModel() is executed

XMLWordPrintable

      How to reproduce:

      $ cat TableSelectionTest.java
      import java.awt.BorderLayout;
      import javax.swing.DefaultListSelectionModel;
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      import javax.swing.event.ListSelectionEvent;
      import javax.swing.event.ListSelectionListener;
      import javax.swing.table.DefaultTableModel;

      public class TableSelectionTest extends JFrame {

        private DefaultTableModel model =
          new DefaultTableModel(
            new String[][] { { "a", "b", "c" } },
            new String[] { "c1", "c2", "c3" }
          );

        public TableSelectionTest() {
          setDefaultCloseOperation(DISPOSE_ON_CLOSE);
          getContentPane().setLayout(new BorderLayout());

          final JTable table = new JTable();

          getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
          table.getSelectionModel().setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION);
          table.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
              public void valueChanged(ListSelectionEvent e) {

                // we need the contents of the cells in the first column, when selection changes:
                for (int i = e.getFirstIndex(); i <= e.getLastIndex(); i++) {
                    try {
                      System.out.println(table.getValueAt(i, 0));
         } catch (RuntimeException ex) {
                      System.err.println("table.columCount = " + table.getColumnCount());
                      System.err.println("table.rowCount = " + table.getRowCount());
                      System.err.println("tableModel.columCount = " + table.getModel().getColumnCount());
                      System.err.println("tableModel.rowCount = " + table.getModel().getRowCount());
                      throw ex;
                    }
                  }
              }
            }
          );

          table.setModel(model);

          pack();
          setVisible(true);
        }

        public static void main(String[] args) {
          TableSelectionTest tableselectiontest = new TableSelectionTest();
        }
      }

      $ source ~/.java14
      $ java -version
      java version "1.4.2_08"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
      Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
      $ javac TableSelectionTest.java
      $ java TableSelectionTest

      // There is no problem, just exit the application.
      // Let's try it with latest 5.0 now

      $ source ~/.java15
      $ java -version
      java version "1.5.0_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
      Java HotSpot(TM) Server VM (build 1.5.0_04-b05, mixed mode)
      $ java TableSelectionTest
      table.columCount = 0
      table.rowCount = 1
      tableModel.columCount = 3
      tableModel.rowCount = 1
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
              at java.util.Vector.elementAt(Vector.java:432)
              at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:280)
              at javax.swing.JTable.convertColumnIndexToModel(JTable.java:1761)
              at javax.swing.JTable.getValueAt(JTable.java:1852)
              at TableSelectionTest$1.valueChanged(TableSelectionTest.java:33)
              at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:187)
              at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
              at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:214)
              at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:408)
              at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:417)
              at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:510)
              at javax.swing.DefaultListSelectionModel.removeSelectionInterval(DefaultListSelectionModel.java:482)
              at javax.swing.JTable.checkLeadAnchor(JTable.java:2965)
              at javax.swing.JTable.tableChanged(JTable.java:2993)
              at javax.swing.JTable.setModel(JTable.java:2827)
              at TableSelectionTest.<init>(TableSelectionTest.java:46)
              at TableSelectionTest.main(TableSelectionTest.java:53)

      // Let's try it with the 5.0 GA:

      $ source ~/.java150
      $ java -version
      java version "1.5.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
      Java HotSpot(TM) Server VM (build 1.5.0-b64, mixed mode)
      $ java TableSelectionTest
      table.columCount = 0
      table.rowCount = 1
      tableModel.columCount = 3
      tableModel.rowCount = 1
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
              at java.util.Vector.elementAt(Vector.java:432)
              at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:280)
              at javax.swing.JTable.convertColumnIndexToModel(JTable.java:1761)
              at javax.swing.JTable.getValueAt(JTable.java:1852)
              at TableSelectionTest$1.valueChanged(TableSelectionTest.java:33)
              at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:187)
              at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
              at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:214)
              at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:408)
              at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:417)
              at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:510)
              at javax.swing.DefaultListSelectionModel.removeSelectionInterval(DefaultListSelectionModel.java:482)
              at javax.swing.JTable.checkLeadAnchor(JTable.java:2965)
              at javax.swing.JTable.tableChanged(JTable.java:2993)
              at javax.swing.JTable.setModel(JTable.java:2827)
              at TableSelectionTest.<init>(TableSelectionTest.java:46)
              at TableSelectionTest.main(TableSelectionTest.java:53)

      => It is the GA release which introduces the problem.

            shickeysunw Shannon Hickey (Inactive)
            jloefflm Johann Löfflmann (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: