Forums Register Login

JFileChooser cannot choose a root directory

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2831 times.
Similar Threads
check out the create New Folder button in JFileChooser
file explorer
DirectoryChooser - How?
Save XML nodes in a String array for ChooseNodes
JDK1.5 installation failed on ubuntu linux 11.10 server
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 10:08:14.