0

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 -

enter image description here

However, sometimes, it appears as - enter image description here

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.

Community
  • 1
  • 1
user2689782
  • 747
  • 14
  • 31
  • 2
    Can you post an [MCVE](http://stackoverflow.com/help/mcve) showing us exactly what you're doing? What are you setting as the default file when you show it? – Kevin Workman Mar 24 '14 at 18:21
  • I ran your code and I never got what you "sometimes" got. Are you doing something special for the messed up file chooser to appear? – user1803551 Mar 25 '14 at 14:04

0 Answers0