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

JFileChooser within a component ignores the Enter key

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.7.0_45"
      Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
      Java HotSpot(TM) Client VM (build 24.45-b08, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      While using JFileChooser as a component within a JPanel, JFrame or JDialog, pressing the Enter key does not confirm a file nor does it allow entering a directory. All other keys (arrow keys to navigate, escape key, F2 to rename) work as expected. Enter key works properly when you confirm a rename. It just does not allow you to enter a directory or confirm a file.

      Here are some interesting observations from our testing:
      - it works in Java 6 releases we tested
      - it does not work in Java 7 releases (tested on 1.7.0_25, 1.7.0_40, 1.7.0_45)
      - it works when you use a stand-alone JFileChooser using its showOpenDialog method
      - it works when you use NimbusLookAndFeel, but does not work when using any other look & feel (Metal, Motif, System...)

      REGRESSION. Last worked in version 6u43

      ADDITIONAL REGRESSION INFORMATION:
      java version "1.6.0_31"
      Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
      Java HotSpot(TM) Client VM (build 20.6-b01, mixed mode, sharing)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the attached test case - try navigating the JFileChooser using keyboard (i.e. pressing enter to navigate into subdirectories). You can switch the t.testDialog() call to false to see that this works when using JFileChooser.showOpenDialog, or uncomment the setLookAndFeel calls to test with various look & feel settings.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected result is for the Enter key to work consistently in the JFileChooser.
      ACTUAL -
      Enter key does not allow you to enter a directory or confirm a file.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.Frame;
      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;

      import javax.swing.JDialog;
      import javax.swing.JFileChooser;
      import javax.swing.JFrame;

      public class FileChooserEnterIssue
      {
      public static void main(String[] args)
      {
      try
      {
      //UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
      //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
      //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
      }
      catch (Exception e)
      {
      e.printStackTrace();
      }

      FileChooserEnterIssue t = new FileChooserEnterIssue();
      t.testDialog(true); // true to use within JDialog, false to use plain JFileChooser
      }

      public void testDialog(boolean useDialog)
      {
      JFrame f = new JFrame();
      JFileChooser chooser = new JFileChooser();

      if(useDialog) // use a JDialog instance encapsulating a JFileChooser instance
      {
      JDialog dlg = new FileChooserDialog(f, chooser);
      dlg.pack();
      dlg.setVisible(true);
      }
      else // use a standalone JFileChooser
      chooser.showOpenDialog(f);
              
              f.dispose();
      }

      private final class FileChooserDialog extends JDialog implements ActionListener
      {
          public FileChooserDialog(Frame owner, JFileChooser fileChooser)
          {
              super(owner, "File Chooser Test", true); // modal
              fileChooser.addActionListener(this);
              this.add(fileChooser);
          }
         
          public void actionPerformed(ActionEvent event)
          {
              setVisible(false);
          }
      }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      So far, I did not find a workaround.

            alexsch Alexandr Scherbatiy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: