1

Can I redirect the JFileChooser's path to My resource directory in my application?

djot
  • 2,952
  • 4
  • 19
  • 28
Hanks
  • 19
  • 2
  • 6
  • It would be helpful if you were more specific about where the resource directory is, as in relative to your application or... ? – Charles Goodwin Jul 04 '11 at 11:25
  • You mean set a directory and don't allow the user to change it, i.e. browser other directories? – Thomas Jul 04 '11 at 12:20

5 Answers5

3

You can define your JFileChooser to start in a specified directory:

JFileChooser fileChooser = new JFileChooser(new File("your directory")) 
Giann
  • 3,142
  • 3
  • 23
  • 33
2

You probably want:

// returns the current working directory as a String
System.getProperty("user.dir");

In conjunction with JFileChooser instantiation:

String workingdir = System.getProperty("user.dir");
JFileChooser fileChooser = new JFileChooser(new File(workingdir));

Just in case the above doesn't always return the directory you want (I would try running the application from a few different locations) then there are some alternatives here:

Get the application's path

Community
  • 1
  • 1
Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
1

You can do it either like this (as Giann mentioned):

JFileChooser fileChooser = new JFileChooser(new File("your directory"));

or

JFileChooser fileChooser = new JFileChooser().setCurrentDirectory(new File("your directory"));
// or in 2 lines
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File("your directory"));
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

Use JFileChooser object's setCurrentDirectory(File dir) method, set you directory File object in the paremeter. Then you make it.

Baz
  • 36,440
  • 11
  • 68
  • 94
sanren1024
  • 37
  • 5
0

I'm not quite sure I fully understand what you're trying to achieve, but maybe you could wrap FileSystemView and override getParentDirectory(...) to return the same directory again. Then call setFileSystemView(...) on the JFileChooser. Note, however, that there are platform dependent subclasses of FileSystemView and you should really be careful about messing around with this.

Edit: there might be other ways as well, e.g. overriding JFileChooser#setCurrentDirectory() or creating your own UI but I'd not recommend pursuing any of those, not even the FileSystemView method.


An option might be to create your own dialog and just display the files of one directory. That might be easier and at least more maintainable, since the implementation should be quite straightforward.

Thomas
  • 87,414
  • 12
  • 119
  • 157