Results 1 to 3 of 3
  1. #1
    munkenmads is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default Wrong currentDirectory with JFileChooser

    Hi

    I'm working on this program that has to get the names of the pictures in a folder and save some text files in another folder. When i try to get the path to the directory the pictures are in, using JFileChooser, the getCurrentDirectory() returns the folder that contains the picture folder instead of the folder chosen in the dialog. Ex:
    I choose the folder C:/Documents/Images
    The returned string is C:/Documents

    When I change the SelectionMode and choose a file there's no problem in returning the right path, but i want to use this for saving too, and there might not be any files in that folder.
    My code:
    j = new JFileChooser();
    j.setFileSelectionMode(JFileChooser.DIRECTORIES_ON LY);
    j.showOpenDialog(null);
    String path = j.getCurrentDirectory();
    System.out.println("Current directory: "+path);

    My solution has been to add the name of the folder to path, but the picture folder might not have the same name everytime.

    My next question is what to do when i have the right directory and wants to run through the files in there? The files are numbered, but starts somewhere in the 150's and goes on till the 190's, missing some of the numbers.
    My solution to this has been to run from the first number to the last number, but it would be more efficient to leave all the blank numbers out and just look at the actual files.

    Hope some of you might be able to help me on these.
    Mads

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    First, j.getCurrentDirectory(); returns a File-object not a String!
    Second, i think you are looking for j.getSelectedFile() ! (JFileChooser (Java Platform SE 6))

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    My next question is what to do when i have the right directory and wants to run through the files in there? The files are numbered, but starts somewhere in the 150's and goes on till the 190's, missing some of the numbers.
    My solution to this has been to run from the first number to the last number, but it would be more efficient to leave all the blank numbers out and just look at the actual files.
    Read the API for File, looking for methods that return an array.

    db

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. jfilechooser
    By ranadav in forum AWT / Swing
    Replies: 7
    Last Post: 06-03-2010, 02:44 PM
  3. Jfilechooser
    By greatmajestics in forum Java 2D
    Replies: 5
    Last Post: 03-30-2010, 06:13 PM
  4. using jfilechooser
    By jagadhguru in forum AWT / Swing
    Replies: 3
    Last Post: 10-30-2008, 12:17 PM
  5. Need JFileChooser Help
    By Wraithier in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 05:40 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
  •