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

JFileChooser: selecting a new Filter deletes filename in fileNameTextField

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.2, 5.0
    • client-libs
    • x86, sparc
    • linux, solaris_9



      Name: gm110360 Date: 08/31/2004


      FULL PRODUCT VERSION :
      java version "1.4.2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
      Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux secret 2.4.20-xfs-backstreet-ruby #7 SMP Wed Mar 19 14:43:53 MET 2003 i686 unknown

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      standard

      A DESCRIPTION OF THE PROBLEM :
      When one chooses another FileFilter from the FilterComboBox, at least the
      MetalFileChooserUI's (as well as the WindowsLookAndFeel FileChooser's UI)
      setSelectedItem(Object filter) does a setFileName(null) after calling setFileFilter((FileFilter) filter).

      This is extremly annoying for all users and we got several requests
      from our customers to change this (actually most of them are using
      windows, where normal applications do not show such a strange behavior)!

      Changing the filter has in principal nothing to do with "what the filenameTextField" displays. It is just about changing the FileView!
      So please, remove the setFileName(null) from the combobox listener!

      With the current misbehavior one can't build "smart" filechooser like:
      <pre>

      public class SmartFileChooser extends JFileChooser {

              public SmartFileChooser(..., ...) {
                   super(...,...)
              }

      public void setFileFilter(FileFilter filter) {
      File f = getSelectedFile();
      String txt = null;
      if (f != null) {
      FileChooserUI ui = this.getUI();
      if (ui instanceof BasicFileChooserUI) {
      txt = ((BasicFileChooserUI) ui).getFileName();
      }
      }
      super.setFileFilter(filter);
      if (f != null && filter != null && txt != null) {
      FileChooserUI ui = this.getUI();
      if (! (ui instanceof BasicFileChooserUI)) {
      return;
      }
      int idx = txt.lastIndexOf(".");
      if (idx != -1) {
      txt = txt.substring(0, idx+1);
      if (filter instanceof GenericFileFilter) {
      txt += ((GenericFileFilter) filter).getExtension();
      }
      }
      setSelectedFile(new File(txt));
      ((BasicFileChooserUI) ui).setFileName(txt);
      }
      }
      }

      </pre>

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Setup a JFileChooser with different ChoosableFileFilters,
      set a selected File (e.g. file.xml) and fire up the dialog.

      Now change the filter via combobox to another file filter.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The fileNameTextField (above the file type aka filter combobox) should
      still contain the filename that was used via the latest setSelectedFile(file) call.
      ACTUAL -
      The text in the fileNameTextField was deleted.

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      public class Fc {
      static class GenericFileFilter extends FileFilter {
      private String extension = null;
      private String description = null;
      public GenericFileFilter(String ext, String description) {
      this.extension = ext;
      this.description = description;
      }

      public boolean accept(File file) {
      String ext = file.getName();
      int i = ext.lastIndexOf('.');
      if (i > 0 && i < ext.length() - 1) {
      ext = ext.substring(i+1).toLowerCase();
      }
      if (ext.equals(this.extension)) {
      return true;
      }
      return false;
      }

      public String getExtension() {
      return this.extension;
      }

      public String getDescription() {
      return this.description;
      }
      // ...
      }

      public static void main(String[] args) {
      final JFileChooser fc = new JFileChooser();
      // Note: source for ExampleFileFilter can be found in FileChooserDemo,
      // under the demo/jfc directory in the Java 2 SDK, Standard Edition.
      FileFilter filter = new GenericFileFilter("gif", "GIF Images");
      fc.addChoosableFileFilter(filter);
      filter = new GenericFileFilter("jpg", "JPEG Images");
      fc.addChoosableFileFilter(filter);
      fc.setSelectedFile(new File("file.xml"));
      int returnVal = fc.showOpenDialog(null);
      System.exit(returnVal);
      }
      }

      ---------- END SOURCE ----------
      (Incident Review ID: 225827)
      ======================================================================

      This bug is reproducible in jse1.5.0 b63 with GTKLookAndFeel also.

      ###@###.### 2004-09-01

            leifs Leif Samuelsson (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: