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

JFileChooser "loses" default directory

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 1.4.0
    • Fix Version/s: 1.4.1
    • Component/s: client-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      hopper
    • CPU:
      x86
    • OS:
      windows_98
    • Verification:
      Verified

      Description



      Name: gm110360 Date: 09/27/2001


      java version "1.4.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
      Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)

      Under JSE 1.3, the following code displays a File Chooser Dialog that shows the
      files in the directory "C:\JavaPrograms\". Under 1.4, the directory defaults
      to "C:\My Documents\", unless you revise the line:
       
           d.setSelectedFile(new File("foobar.txt"));

      to the form:

            d.setSelectedFile(new File("C:\JavaPrograms", "foobar.txt"));

      import java.awt.*;
      import java.awt.event.*;
      import java.awt.List;
      import java.io.*;
      import java.util.*;
      import java.awt.print.*;
      import javax.swing.*;
      import javax.swing.filechooser.*;
      import javax.swing.event.*;


      public class FileChooserTest
      { static JFrame f = new JFrame();

         public static void main(String [] args)
         { f.setBounds(0, 0, 600, 600);
            f.setTitle("File Chooser Test");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
            Container content_pane = f.getContentPane();
            

            JFileChooser d;
              
            String fname;
            
            d = new JFileChooser();
            d.setDialogTitle("Read Saved Schedule From");
            d.setCurrentDirectory(new File("C:\\JavaPrograms\\"));
            SaveFilter sf = new SaveFilter();
            d.addChoosableFileFilter(sf);
            d.setFileFilter(sf);
            d.setSelectedFile(new File("foobar.txt"));

            if (d.showOpenDialog(content_pane) == JFileChooser.APPROVE_OPTION)
               fname = d.getSelectedFile().getAbsolutePath();
             else
               fname = null;

         }
      }

      class SaveFilter extends javax.swing.filechooser.FileFilter
      { public boolean accept(File f)
         { String ext;
         
            if (f.isDirectory())
               return true;
               
            ext = getExtension(f);
            
            if (ext == null)
               return false;
            if (ext.equals("sch"))
               return true;
            
            return false;
         }
         
         public String getDescription()
         { return "Saved Schedules (*.sch)";
         }

         public String getExtension(File f)
         { String ext = null;
         
            String s = f.getName();
            
            int i = s.lastIndexOf('.');

            if (i > 0 && i < s.length() - 1)
               ext = s.substring(i+1).toLowerCase();
              
            return ext;
         }
         
      }
      (Review ID: 132730)
      ======================================================================

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: