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

JFileChooser can throw IndexOutOfBoundsException as selections change

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_07"
      Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
      Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      JFileChooser can throw IndexOutOfBoundsException in the normal course of business, as files are selected and the current directory changes or files in the currently selected directory change. This seems to be a quite different stack trace from the other reported JFileChooser/IOOBE bugs. The example is rather contrived and only works when the file chooser is not visible (probably because of timing), but we occasionally get stack dumps from our users with the top three frames identical (or nearly so) and I have experienced it several times myself. Typically it seems to happen when the file chooser was just made visible (redisplayed), the current directory has just been rescanned, the files in that directory have changed, and the user clicks the choose button (open, save, or whatever), but I don't know how much of that is significant.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      create a C:\temp directory and populate with a small number of files (like five).
      create a C:\temp2 directory and populate with a large number of files (like 1500).
      run code

      If that doesn't work, try changing the time delay.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expect no output.
      ACTUAL -
      There are numerous identical stack dumps.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Stack dump from example program:

      Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Invalid index
        at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:497)
        at sun.swing.FilePane$SortableListModel.getElementAt(FilePane.java:526)
        at javax.swing.JList.getSelectedValues(JList.java:2234)
        at sun.swing.FilePane.setFileSelected(FilePane.java:1346)
        at sun.swing.FilePane.doSelectedFilesChanged(FilePane.java:1449)
        at sun.swing.FilePane.propertyChange(FilePane.java:1506)
        at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:339)
        at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:276)
        at java.awt.Component.firePropertyChange(Component.java:7868)
        at javax.swing.JFileChooser.setSelectedFiles(JFileChooser.java:511)
        at JFileChooserBug15.change(JFileChooserBug15.java:56)
        at JFileChooserBug15$2$1.run(JFileChooserBug15.java:36)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

      Stack dump from "real world" program:

      java.lang.IndexOutOfBoundsException: Invalid index
      at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:497)
      at sun.swing.FilePane$SortableListModel.getElementAt(FilePane.java:526)
      at javax.swing.JList.getSelectedValue(JList.java:2276)
      at javax.swing.plaf.basic.BasicFileChooserUI$Handler.valueChanged(BasicFileChooserUI.java:499)
      at javax.swing.JList.fireSelectionValueChanged(JList.java:1765)
      at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1779)
      at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
      at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:137)
      at javax.swing.DefaultListSelectionModel.setValueIsAdjusting(DefaultListSelectionModel.java:668)
      at javax.swing.JList.setValueIsAdjusting(JList.java:2110)
      at javax.swing.plaf.basic.BasicListUI$Handler.mouseReleased(BasicListUI.java:2778)
      at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:273)
      at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
      at java.awt.Component.processMouseEvent(Component.java:6041)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
      at java.awt.Component.processEvent(Component.java:5806)
      at java.awt.Container.processEvent(Container.java:2058)
      at java.awt.Component.dispatchEventImpl(Component.java:4413)
      at java.awt.Container.dispatchEventImpl(Container.java:2116)
      at java.awt.Component.dispatchEvent(Component.java:4243)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
      at java.awt.Container.dispatchEventImpl(Container.java:2102)
      at java.awt.Window.dispatchEventImpl(Window.java:2440)
      at java.awt.Component.dispatchEvent(Component.java:4243)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


      REPRODUCIBILITY :
      This bug can be reproduced occasionally.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.io.File;

      public class JFileChooserBug15 {
       
         static JFileChooser fc;
         static int count;
       
         public static void main(String[] args) {
            SwingUtilities.invokeLater(
               new Runnable() {
                  public void run() {
                     test();
                  }
               });
         };
         
         
         static void test() {
            fc = new JFileChooser();
            fc.setMultiSelectionEnabled(true);
            Runnable r =
               new Runnable() {
                  public void run() {
                     for (int i = 0; i < 1000; i++) {
                        try {
                           Thread.sleep(100);
                        }
                           catch (InterruptedException e) {
                           }
                        SwingUtilities.invokeLater(
                              new Runnable() {
                                 public void run() {
                                    change();
                                 }
                              }
                           );
                     }
                  }
               };
            new Thread(r).start();
         }
         
         
         public static void change() {
            count++;
            if (count % 3 == 0) {
               fc.setCurrentDirectory(new File("C:\\temp"));
            }
            else {
               File dir = new File("C:\\temp2");
               fc.setCurrentDirectory(dir);
               File selectedFile = new File(dir, "tmp");
               fc.setSelectedFiles(dir.listFiles());
            }
         }
      }

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

            rupashka Pavel Porvatov (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: