15

I am trying to set the directory path in JFilechooser through something like this(using commons-io ) :

String fileContents = IOUtils.toString(new FileInputStream("path.txt"));
File theDirectory = new File(fileContents);

filechooser = new JFileChooser();
fileChooser.setCurrentDirectory(theDirectory);
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

I'm using getCanonicalPath() to get the path and write in the file path.txt

path = file.getCanonicalPath();

I don't intend to put all my code here,but I'm sure that the program writes and reads the path in path.txt. I don't get any error,but everytime I run the program it always open JFilechooser in my documents folder.What i am doing wrong?

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
fermk090
  • 151
  • 1
  • 1
  • 3
  • Have you tried printing out `theDirectory` and whether it exists? `System.out.println( theDirectory.getCanonicalPath() + " exists: " + theDirectory.exists() );` If the file no longer exists, then the file chooser will default to the My Documents folder. – wolfcastle Jun 22 '11 at 17:23

6 Answers6

22

Try to pass the current directory directly in the constructor:

filechooser = new JFileChooser(theDirectory);
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
6

If you consult the API, using the default constructor (i.e. new JFileChooser()):

Constructs a JFileChooser pointing to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.

This would seem to account for always opening to My Documents, but this isn't your problem. In fact, your problem lies with setting the current directory (i.e. setCurrentDirectory(theDirectory)):

Sets the current directory. Passing in null sets the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix. If the file passed in as currentDirectory is not a directory, the parent of the file will be used as the currentDirectory. If the parent is not traversable, then it will walk up the parent tree until it finds a traversable directory, or hits the root of the file system.

That being said, I'd pay attention to the highlighted text since it appears that you're setting a file as the current directory and not a directory.

mre
  • 43,520
  • 33
  • 120
  • 170
  • I'm not setting a file as the current directory.I'm trying to set the current directory with the last directory selected File file = fileChooser.getSelectedFile(); What i'm tryng to do is always open filechooser with the last directory selected even if i close the program and run it again – fermk090 Apr 20 '11 at 16:25
  • 1
    @fermk090: then you're going to have to persist the `last directory selected` and when you run the program again, load the persisted directory. – mre Apr 20 '11 at 17:40
  • I think that is what he is doing. He's persistinting the location into 'path.txt' and recovering it. – wolfcastle Jun 22 '11 at 17:20
1

In your main class declare

public static String dirpath=".";

private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {    
 JFileChooser jfc = new JFileChooser(dirpath);
 dirpath =jfc.getSelectedFile().getAbsolutePath().toString();
}
Nikita Silverstruk
  • 1,097
  • 1
  • 20
  • 42
1

For select the last directory that you open :

chooser.setCurrentDirectory(lastDirectory);

int r = chooser.showOpenDialog(new JPanel());

if (r == JFileChooser.APPROVE_OPTION) {
   fileName = chooser.getSelectedFile().getPath();
   lastDirectory = chooser.getSelectedFile();
}
Batuhan B
  • 1,835
  • 4
  • 29
  • 39
1

JFileChooser Chooser = new JFileChooser("F:");

A.Mohamed Bilal
  • 115
  • 1
  • 13
0

if you want to change the directory theb use System.getProperty method

String s=System.getProperty("user.dir");  // changes directory from documents to the user current Directory;

JFileChooser jfc=new JFileChooser(s);