JFileChooser, want to lock it to one directory

后端 未结 3 1252
半阙折子戏
半阙折子戏 2020-12-11 09:17

I have this program where u can download files and i want the JFileChooser to be locked to one folder(directory) so that the user cant browse anything else. He can only choo

3条回答
  •  一生所求
    2020-12-11 09:55

    Set a FileView and override the isTraversable method so that it returns true only for the directory you want the user to see.

    Here is an example:

    String getProperty = System.getProperty("user.home");
    final File dirToLock = new File(getProperty + "/Dropbox/Prosjekt RMI/SERVER/");
    JFileChooser fc = new JFileChooser(dirToLock);
    fc.setFileView(new FileView() {
        @Override
        public Boolean isTraversable(File f) {
             return dirToLock.equals(f);
        }
    });
    

提交回复
热议问题