This is a question that has been asked before, but there is a bug in that solution.
My requirements are - when user invokes JFileChooser, he should not be able to navigate up the tree.
The answer has been to write a custom FileSystemView - How do I restrict JFileChooser to a directory? Has a solution, as does http://tips4java.wordpress.com/2009/01/28/single-root-file-chooser/
However, both of them have a problem sometimes. For the correct implementation, the FileChooser should come up as -
However, sometimes, it appears as -
and user has to click the up arrow to see the correct directory. Any idea why this is happening please? And a solution ?
The code for the custom FileSystemView is
public class SingleRootFileSystemView extends FileSystemView
{
File root;
File[] roots = new File[1];
public SingleRootFileSystemView(File root)
{
super();
this.root = root;
roots[0] = root;
}
@Override
public File createNewFolder(File containingDir)
{
File folder = new File(containingDir, "New Folder");
folder.mkdir();
return folder;
}
@Override
public File getDefaultDirectory()
{
return root;
}
@Override
public File getHomeDirectory()
{
return root;
}
@Override
public File[] getRoots()
{
return roots;
}
}
The code to invoke this is
FileSystemView fsv = new SingleRootFileSystemView(folder);
JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory(),fsv);
fileChooser.setFileSelectionMode(1);
int returnVal = fileChooser.showOpenDialog(null);
Thanks very much.