<p>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.</p>

<p>How should I go about doing that?</p>

[{'answer_id': 32549, 'author': 'McDowell', 'author_id': 304, 'author_profile': 'https://Stackoverflow.com/users/304', 'pm_score': 5, 'selected': True, 'text': '<p>You can probably do this by setting your own <a href="https://docs.oracle.com/javase/8/docs/api/javax/swing/filechooser/FileSystemView.html" rel="noreferrer">FileSystemView</a>.</p>\n'}, {'answer_id': 32658, 'author': 'Allain Lalonde', 'author_id': 2443, 'author_profile': 'https://Stackoverflow.com/users/2443', 'pm_score': 5, 'selected': False, 'text': '<p>Incase anyone else needs this in the future:</p>\n\n<pre><code>class DirectoryRestrictedFileSystemView extends FileSystemView\n{\n    private final File[] rootDirectories;\n\n    DirectoryRestrictedFileSystemView(File rootDirectory)\n    {\n        this.rootDirectories = new File[] {rootDirectory};\n    }\n\n    DirectoryRestrictedFileSystemView(File[] rootDirectories)\n    {\n        this.rootDirectories = rootDirectories;\n    }\n\n    @Override\n    public File createNewFolder(File containingDir) throws IOException\n    {       \n        throw new UnsupportedOperationException("Unable to create directory");\n    }\n\n    @Override\n    public File[] getRoots()\n    {\n        return rootDirectories;\n    }\n\n    @Override\n    public boolean isRoot(File file)\n    {\n        for (File root : rootDirectories) {\n            if (root.equals(file)) {\n                return true;\n            }\n        }\n        return false;\n    }\n}\n</code></pre>\n\n<p>You\'ll obviously need to make a better "createNewFolder" method, but this does restrict the user to one of more directories.</p>\n\n<p>And use it like this:</p>\n\n<pre><code>FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("X:\\\\"));\nJFileChooser fileChooser = new JFileChooser(fsv);\n</code></pre>\n\n<p>or like this:</p>\n\n<pre><code>FileSystemView fsv = new DirectoryRestrictedFileSystemView( new File[] {\n    new File("X:\\\\"),\n    new File("Y:\\\\")\n});\nJFileChooser fileChooser = new JFileChooser(fsv);\n</code></pre>\n'}, {'answer_id': 6852912, 'author': 'mlh', 'author_id': 866528, 'author_profile': 'https://Stackoverflow.com/users/866528', 'pm_score': 3, 'selected': False, 'text': '<p>The solution of Allain is almost complete. Three problems are open to solve:</p>\n\n<ol>\n<li>Clicking the "Home"-Button kicks the user out of restrictions</li>\n<li>DirectoryRestrictedFileSystemView is not accessible outside the package</li>\n<li>Starting point is not Root</li>\n</ol>\n\n<hr>\n\n<ol>\n<li>Append @Override to DirectoryRestrictedFileSystemView</li>\n</ol>\n\n<p><code>public TFile getHomeDirectory()\n{\n  return rootDirectories[0];\n}</code></p>\n\n<ol>\n<li><p>set class and constructor <code>public</code></p></li>\n<li><p>Change <code>JFileChooser fileChooser = new JFileChooser(fsv);</code> into <code>JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory(),fsv);</code></p></li>\n</ol>\n\n<p>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.</p>\n'}, {'answer_id': 12244107, 'author': 'Siddharth Tyagi', 'author_id': 1052227, 'author_profile': 'https://Stackoverflow.com/users/1052227', 'pm_score': -1, 'selected': False, 'text': '<p>No need to be that complicated. You can easily set selection mode of a JFileChooser like this</p>\n\n<pre><code>JFileChooser fc = new JFileChooser();\nfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);\nfc.setMultiSelectionEnabled(false);\n</code></pre>\n\n<p>You can read more reference here <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html" rel="nofollow">How to Use File Choosers</a></p>\n'}]
['https://Stackoverflow.com/questions/32529', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2443/']