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

JFileChooser: Enter key always mapped to open button

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.4.1
    • client-libs



      Name: wm7046 Date: 11/17/2003


      Steps to reproduce:
      1. Compile and run the testcase
          javac -d . DialogTest.java
          java an.bn.DialogTest
      2. Press the Button "Open the File" which will bring up the JFileChooser.
      3. Seelct one of the modules and then press <tab> till the focus goes to cancel button.
      4. Now press <enter>.
      5. The expected behaviour: Cancel should have been activated.
          but the actual behaviour is: The module has got selected and you will see a printf indicating the module selected. This indicates that by pressing <enter> even when the focus is on the Cancel button, the Open button was activated.

      Testcase:
      DialogTest.java

      package an.bn;
      import java.io.*;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import javax.swing.filechooser.*;
      import java.util.StringTokenizer;

      public class DialogTest extends JFrame {
          static private final String newline = "\n";
          JFileChooser fc = null;

          public DialogTest() {
              super("FileChooserDemo");

              //Create the log first, because the action listeners
              //need to refer to it.

              //Create a file chooser
              fc = new JFileChooser();
              fc.setMultiSelectionEnabled(true);
              TextFilter txt = new TextFilter();
              fc.setFileFilter(txt);
              fc.addChoosableFileFilter(txt);
              fc.setCurrentDirectory(new File(System.getProperty("user.dir")));


              //Create the open button
              JButton openButton = new JButton("Open a File...");
              openButton.setMnemonic(openButton.getText().charAt(0));
              openButton.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      int returnVal = fc.showOpenDialog(null);

                      if (returnVal == JFileChooser.APPROVE_OPTION) {
                          System.out.println("Module selected " + newline);
                      }
                      else {
                          System.out.println("Open command cancelled by user." + newli
      ne);
                      }
                  }
              });

              //For layout purposes, put the buttons in a separate panel
              JPanel buttonPanel = new JPanel(new FlowLayout());
              buttonPanel.add(openButton);

              //Add the buttons and the log to the frame
              Container contentPane = getContentPane();
              contentPane.add(buttonPanel);
          }
          public static void main(String[] args) {
              StringBuffer tm=new StringBuffer("\"");
              tm.append("THis is new");
              tm.append("\"");
                      System.out.println(tm);
              JFrame frame = new DialogTest();

              frame.addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }

              });

              frame.pack();
              frame.setVisible(true);
          }

      class TextFilter extends javax.swing.filechooser.FileFilter
      {
              public boolean accept(File f)
              {
                      if ( f.isDirectory() || f.getName().endsWith("txt") || f.getName
      ().endsWith("TXT") )
                              return true;
                      else
                              return false;
              }

              public String getDescription()
              {
                      return "Text Files";
              }
      }

      }
      (Review ID: 215642)
      ======================================================================

            kereminsunw Konstantin Eremin (Inactive)
            mmma Marvin Ma (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: