Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By DarrylBurke

Thread: How to grab directory from a JFileChooser?

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

    Default How to grab directory from a JFileChooser?

    When I use a JFileChooser, I wish it would "remember" the last directory opened, but it always reverts back to \MyDocuments or something (where I NEVER place anything). I have to bubble up a gazillion levels and then back down again to locate the file I want.

    Does the JFileChooser make this info available anywhere, so that I can store it for subsequent FileOpen (or Save) operations?

    IOW, in addition to:

    Java Code:
    fileDisposition = fc.getSelectedFile();
    it would be nice if there was something like:
    Java Code:
    fileDisposition = fc.getSelectedDirectory();
    (fc is a JFileChooser, and fileDisposition is a File)

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    A directory is a File. So, you can just use getSelectedFile() method.

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

    Default

    Quote Originally Posted by camickr View Post
    A directory is a File. So, you can just use getSelectedFile() method.
    What I got back from getSelectedFile() is the filename only, no path info. This code:

    Java Code:
    fileDispatch = fc.getSelectedFile();
    lblDispatchFileName.setText(String.format(
          "%s%s", lblDispatchFileName.getText(), fileDispatch.getName()));
    sets lblDispatchFileName's text from "Dispatch File = " to "Dispatch File = DispatchFileTest.txt" for example (not "Dispatch File = C:\SorterSim\Data\DispatchFileTest.txt"

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    Read the API doc for the File class. It has lots of methods to get paths.
    The name of a file does not include the path.
    blackbird likes this.

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

    Default

    Quote Originally Posted by Norm View Post
    Read the API doc for the File class. It has lots of methods to get paths.
    The name of a file does not include the path.
    OK, got it:

    Java Code:
    sLastDirOpened = fileDispatch.getAbsolutePath();
    lblDispatchFileName.setText(String.format(
    	"%s%s", lblDispatchFileName.getText(), sLastDirOpened));

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

Similar Threads

  1. how to grab info from array
    By skuzzie in forum New To Java
    Replies: 3
    Last Post: 12-09-2010, 02:26 PM
  2. JFileChooser start in .jar directory
    By Sandia_man in forum AWT / Swing
    Replies: 2
    Last Post: 08-30-2010, 09:15 PM
  3. Trying to login to a website then grab another page
    By DaveTheAve in forum New To Java
    Replies: 6
    Last Post: 04-14-2010, 10:44 PM
  4. grab the textfile
    By Sticks_ll in forum New To Java
    Replies: 3
    Last Post: 03-31-2009, 02:23 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
  •