How do I restrict JFileChooser to a directory?
Asked Answered
C

4

26

I want to limit my users to a directory and its sub directories but the "Parent Directory" button allows them to browse to an arbitrary directory.

How should I go about doing that?

Changeable answered 28/8, 2008 at 15:11 Comment(0)
D
14

You can probably do this by setting your own FileSystemView.

Digester answered 28/8, 2008 at 15:15 Comment(3)
how do you get the default filesystemview (e.g. to delegate to it)?Vally
@Jason S - probably via static method getFileSystemView()Digester
Just if someone could need it, here is an exact example of what OP wanted: tips4java.wordpress.com/2009/01/28/single-root-file-chooserBritton
C
22

Incase anyone else needs this in the future:

class DirectoryRestrictedFileSystemView extends FileSystemView
{
    private final File[] rootDirectories;

    DirectoryRestrictedFileSystemView(File rootDirectory)
    {
        this.rootDirectories = new File[] {rootDirectory};
    }

    DirectoryRestrictedFileSystemView(File[] rootDirectories)
    {
        this.rootDirectories = rootDirectories;
    }

    @Override
    public File createNewFolder(File containingDir) throws IOException
    {       
        throw new UnsupportedOperationException("Unable to create directory");
    }

    @Override
    public File[] getRoots()
    {
        return rootDirectories;
    }

    @Override
    public boolean isRoot(File file)
    {
        for (File root : rootDirectories) {
            if (root.equals(file)) {
                return true;
            }
        }
        return false;
    }
}

You'll obviously need to make a better "createNewFolder" method, but this does restrict the user to one of more directories.

And use it like this:

FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("X:\\"));
JFileChooser fileChooser = new JFileChooser(fsv);

or like this:

FileSystemView fsv = new DirectoryRestrictedFileSystemView( new File[] {
    new File("X:\\"),
    new File("Y:\\")
});
JFileChooser fileChooser = new JFileChooser(fsv);
Changeable answered 28/8, 2008 at 15:57 Comment(0)
D
14

You can probably do this by setting your own FileSystemView.

Digester answered 28/8, 2008 at 15:15 Comment(3)
how do you get the default filesystemview (e.g. to delegate to it)?Vally
@Jason S - probably via static method getFileSystemView()Digester
Just if someone could need it, here is an exact example of what OP wanted: tips4java.wordpress.com/2009/01/28/single-root-file-chooserBritton
S
5

The solution of Allain is almost complete. Three problems are open to solve:

  1. Clicking the "Home"-Button kicks the user out of restrictions
  2. DirectoryRestrictedFileSystemView is not accessible outside the package
  3. Starting point is not Root

  1. Append @Override to DirectoryRestrictedFileSystemView

public TFile getHomeDirectory() { return rootDirectories[0]; }

  1. set class and constructor public

  2. Change JFileChooser fileChooser = new JFileChooser(fsv); into JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory(),fsv);

I use it for restricting users to stay in a zip-file via TrueZips TFileChooser and with slight modifications to the above code, this works perfectly. Thanks a lot.

Swaddle answered 28/7, 2011 at 0:36 Comment(0)
R
-2

No need to be that complicated. You can easily set selection mode of a JFileChooser like this

JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setMultiSelectionEnabled(false);

You can read more reference here How to Use File Choosers

Radome answered 3/9, 2012 at 8:7 Comment(1)
This limits them to directories in general, but not to a specific directory, which is the OP's question.Candra

© 2022 - 2024 — McMap. All rights reserved.