Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JFileChooser cannot choose a root directory  RSS feed

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to present a dialog for a user to choose a directory. One of the most likely directories to want to choose is the root of one drive or another (on Windows); for instance, a user might want to choose the directory "D:\" as their selection.
Although JFileChooser knows about the list of drives and presents its little pull-down list of them, the main box can never contain a root directory, so you can never choose a root directory as your selected file/dir. You can choose "D:\myfiles", for instance, but not "D:\".
Any suggestions how to modify the behaviour?
#8-P
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
Actually it does allow you to do it, but it is not very intuitive.
After you choose the drive, just type in a "/" character into the FileName and all should be well.
Regards,
Manfred.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I found quite a neat solution. I made my own FileSystemView class and used setFileSystemView() on the JFileChooser to activate it.
My FileSystemView class delegates everything to the standard one, except that it adds the current directory (".") to the list of files returned. That means one can select a root directory easily and clearly.
I have only tried this on Windows. I suspect that it works on all UNIX, too, as current directory is "." there, too, but it probably breaks on MacOS etc.
 
Jim Otte
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fantastic Peter- I looked in many forums- but only you had a posted solution to this- thanks for the code posting- saved me lots of time....
I implemented it however with a few changes- which others may want to do- is that with your code the '.' always appears which is great however when pressed it puts client into other directory... I was only wanting a way to list the drives in the chooser so I did this modification on your code-

private static Hashtable rootHash=null;
static{
rootHash = new Hashtable();
File[] roots = FileSystemView.getFileSystemView().getRoots();
for(int i=0;i<roots.length;i++){
rootHash.put(roots[i].toString(), roots[i].toString());
}
...
public File[] getFiles(File dir, boolean useFileHiding){
File[] standardFiles = standardView.getFiles(dir, useFileHiding);
int numStandard = standardFiles.length;
String dirPath = (String)rootHash.get(dir.toString());
if(dirPath==null)
return standardFiles;
File[] files = new File[numStandard + 1];
files[0] = new File(dirPath); // or "." for current dir
System.arraycopy(standardFiles, 0, files, 1, numStandard);
return files;
}
...

This way drives appear at top level dir (with name eg D:\)- and no '.' at any levels to avoid the user dir. problem.....
Once again- great help- thanks
Jim
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!