Results 1 to 8 of 8
Like Tree4Likes
  • 1 Post By JosAH
  • 1 Post By JosAH
  • 1 Post By dlorde
  • 1 Post By dlorde

Thread: What string format for a directory does the JFileChooser expect?

  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default What string format for a directory does the JFileChooser expect?

    Rather than have to navigate up umpteen levels from the default starting folder ("My Documents", where I never put anything) to find the file I want the JFileChooser to open, I want it to start in a given folder. My code is this:

    Java Code:
    JFileChooser fc;
      if (e.getSource().equals(btnOpenDispatchFile)) {
        if (sLastDirOpened.length() > 2) {
          fc = new JFileChooser(sLastDirOpened);
        } else {
          //fc = new JFileChooser("//SorterSimulator/Data/"); <- First attempt; it runs, but still opens in "My Documents"
          fc = new JFileChooser("\\SorterSimulator\Data\");    <- Second attempt - "invalid escape sequence"
          fc = new JFileChooser("\\\SorterSimulator\\Data\\");    <- Third attempt -  ""
          fc = new JFileChooser();
    }
    After the first time, it works fine, but none of the "obvious" things I tried work to create it with a default folder to open. The full dir path is "C:\SorterSimulator\Data\"

    What string do I need for it to open the folder I want?
    Last edited by blackbird; 07-21-2011 at 10:59 PM. Reason: Forgot the question mark

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,421
    Blog Entries
    7
    Rep Power
    26

    Default

    Quote Originally Posted by blackbird View Post
    Rather than have to navigate up umpteen levels from the default starting folder ("My Documents", where I never put anything) to find the file I want the JFileChooser to open, I want it to start in a given folder. My code is this:

    Java Code:
    JFileChooser fc;
      if (e.getSource().equals(btnOpenDispatchFile)) {
        if (sLastDirOpened.length() > 2) {
          fc = new JFileChooser(sLastDirOpened);
        } else {
          //fc = new JFileChooser("//SorterSimulator/Data/"); <- First attempt; it runs, but still opens in "My Documents"
          fc = new JFileChooser("\\SorterSimulator\Data\");    <- Second attempt - "invalid escape sequence"
          fc = new JFileChooser("\\\SorterSimulator\\Data\\");    <- Third attempt -  ""
          fc = new JFileChooser();
    }
    After the first time, it works fine, but none of the "obvious" things I tried work to create it with a default folder to open. The full dir path is "C:\SorterSimulator\Data\"

    What string do I need for it to open the folder I want?
    Either C:\\SorterSimulator\\Data or C:/SorterSimulator/Data or any other combination of a single forward slash or a double backslash ...

    kind regards,

    Jos
    blackbird likes this.
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Either C:\\SorterSimulator\\Data or C:/SorterSimulator/Data or any other combination of a single forward slash or a double backslash ...

    kind regards,

    Jos
    Nope - both run, but both open in "My Documents"

    The folder copied from the Explorer address bar = C:\SorterSimulator\Data

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,421
    Blog Entries
    7
    Rep Power
    26

    Default

    Quote Originally Posted by blackbird View Post
    Nope - both run, but both open in "My Documents"

    The folder copied from the Explorer address bar = C:\SorterSimulator\Data
    Strange, because it works for me (for years already); we have to go through the nitty-gritty details:

    - the directory exists, right?
    - the directory is readable?
    - can you navigate to that directory with a JFileChooser that opened somewhere else?
    - no other code you didn't show us?
    - what is the value of sLastDirOpened?

    kind regards,

    Jos
    Last edited by JosAH; 07-21-2011 at 11:23 PM.
    blackbird likes this.
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Strange, because it works for me (for years already); we have to go through the nitty-gritty details:

    - the directory exists, right?
    - the directory is readable?
    - can you navigate to that directory with a JFileChooser that opened somewhere else?
    - no other code you didn't show us?
    - what is the value of sLastDirOpened?

    kind regards,

    Jos
    Yes
    Yes
    Yes
    No (well, no pertinent code, I don't think)

    sLastDirOpened is explicitly initialized to "";

    The "Inspect window" on sLastDirOpened when I get to that if/else is:

    "sLastDirOpened"= "" (id = 1436)
    count= 0
    hash= 0
    offset= 0
    value= (id=1437)

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    10

    Default

    Both these should be OK - if that directory hierarchy exists on the current drive.
    fc = new JFileChooser("\\SorterSimulator\\Data\\");
    fc = new JFileChooser("\\SorterSimulator\\Data");


    You might consider prefixing the drive letter (e.g. C:) to the path. If the path is incorrect, it will revert to the 'My Documents' directory.

    Plus, are you sure you commented out that last fc = new JFileChooser(); statement before compiling the code?
    blackbird likes this.

  7. #7
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    OK, I guess it had to be exactly this:

    fc = new JFileChooser("\\SorterSimulator\\Data\\");

    Thanks; it saves me a few seconds time each test and gobs of frustration.

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    10

    Default

    Be aware that the path element separators '\\' (known as 'file separators' in Java) vary between operating systems. If you want to make your path literals platform-independent, you can get the current file separator by calling System.getProperties("file.separator");
    blackbird likes this.

Similar Threads

  1. How to grab directory from a JFileChooser?
    By blackbird in forum AWT / Swing
    Replies: 5
    Last Post: 07-16-2011, 08:03 AM
  2. JFileChooser start in .jar directory
    By Sandia_man in forum AWT / Swing
    Replies: 2
    Last Post: 08-30-2010, 09:15 PM
  3. how to use JFileChooser to select file -> string
    By gezzel in forum AWT / Swing
    Replies: 15
    Last Post: 10-23-2008, 06:34 AM
  4. how to use JFileChooser to select file -> string
    By gezzel in forum New To Java
    Replies: 9
    Last Post: 09-18-2008, 10:45 PM
  5. JFileChooser example (selecting a directory)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 01:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •