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

race condition in JFileChooser setSelectedFiles() / setCurrentDirectory()

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_15"
      Java(TM) SE Runtime Environment (build 1.6.0_15-b03)
      Java HotSpot(TM) Client VM (build 14.1-b02, mixed mode, sharing)
      Java SE 6.0 Update 19

      ADDITIONAL OS VERSION INFORMATION :
      Linux ronny-desktop 2.6.31-20-generic #58-Ubuntu SMP Fri Mar 12 05:23:09 UTC 2010 i686 GNU/Linux
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      In my application I have a file chooser where I present the file hierarchy of different increments of a backup directory. The user can select files and switch between the increments. When switching between increments I use the following calls directly in a row:
      JFileChooser.setCurrentDirectory()
      JFileChooser.setSelectedFiles()
      But, unfortunately, the selection was not correctly updated and to my big surprise this construct even let the call to setCurrentDirectory() fail from time to time. I debugged my application but did not find any error on my side. Therefore I wrote a small and very simple demo application which reproduces the bug.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the included demo application. The demo application switches every five seconds between the user home and java home and tries to select all files in these directories. Notice that not all files are selected when the demo program switches to the java home.
      Uncomment the call to sleep() and run the demo application again. Notice that all files are now reliably selected every time the demo application switches to another directory.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expected the demo application to work reliably without the call to sleep().
      ACTUAL -
      The demo application only works reliably with a call to sleep().

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class TestFrame extends JFrame {

          private final String userHome = System.getProperty("user.home");
          private final String javaHome = System.getProperty("java.home");

          public TestFrame() {
              initComponents();
          }

          private void setAndSelect(String path) {
              final File directory = new File(path);
              SwingUtilities.invokeLater(new Runnable() {

                  public void run() {
                      fileChooser.setCurrentDirectory(directory);
                  }
              });
              new Thread() {

                  @Override
                  public void run() {
      // try {
      // sleep(1000);
      // } catch (InterruptedException ex) {
      // }
                      SwingUtilities.invokeLater(new Runnable() {

                          public void run() {
                              fileChooser.setSelectedFiles(directory.listFiles());
                          }
                      });
                  }
              }.start();
          }

          private void initComponents() {

              fileChooser = new JFileChooser();

              setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              addWindowListener(new WindowAdapter() {

                  public void windowOpened(WindowEvent evt) {
                      formWindowOpened(evt);
                  }
              });

              fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
              fileChooser.setMultiSelectionEnabled(true);

              GroupLayout layout = new GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                      layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
                      layout.createSequentialGroup().addContainerGap().addComponent(
                      fileChooser, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                      Short.MAX_VALUE).addContainerGap()));
              layout.setVerticalGroup(
                      layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
                      layout.createSequentialGroup().addContainerGap().addComponent(
                      fileChooser, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
                      Short.MAX_VALUE).addContainerGap()));

              pack();
          }

          private void formWindowOpened(WindowEvent evt) {
              new Thread() {

                  @Override
                  public void run() {
                      for (int i = 0; i < 10; i++) {
                          try {
                              setAndSelect(userHome);
                              Thread.sleep(5000);
                              setAndSelect(javaHome);
                              Thread.sleep(5000);
                          } catch (InterruptedException ex) {
                          }
                      }
                  }
              }.start();
          }

          public static void main(String args[]) {
              java.awt.EventQueue.invokeLater(new Runnable() {

                  public void run() {
                      new TestFrame().setVisible(true);
                  }
              });
          }
          private JFileChooser fileChooser;
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The only workaround I found is a call to sleep() before calling setSelectedFiles(). Unfortunately, the necessary time to sleep depends heavily on the computers speed. Therefore we either waste time by sleeping too long or the workaround fails because sleep() returned too fast.

            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: