JFileChooser, want to lock it to one directory

天涯浪子 提交于 2019-11-28 05:02:59

问题


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 choose files from for example the folder, "C:\Users\Thomas\Dropbox\Prosjekt RMI\SERVER\". I have tried so search but did not find anything. The code I have is:

String getProperty = System.getProperty("user.home");
JFileChooser chooser = new JFileChooser(getProperty + "/Dropbox/Prosjekt RMI/SERVER/"); //opens in the directory "//C:/Users/Thomas/Dropbox/Project RMI/SERVER/"
int returnVal = chooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());

And this is working fine, but now i can go to the folder Project RMI, that i don't want it to do.

Thanks in Advance :)

Edit: What I did with your help:

JFileChooser chooser = new JFileChooser(getProperty + "/Dropbox/Project RMI/SERVER/"); 
                chooser.setFileView(new FileView() {
                    @Override
                    public Boolean isTraversable(File f) {
                        return (f.isDirectory() && f.getName().equals("SERVER")); 
                    }
                });
                int returnVal = chooser.showOpenDialog(parent);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    System.out.println("You chose to open this file: "
                            + chooser.getSelectedFile().getName());
                }

回答1:


Make a custom FileSystemView, use it as the argument to one of the JFileChooser constructors that accepts an FSV..




回答2:


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);
    }
});



回答3:


In my case I needed to disable both directory navigation and choosing a different file extension Here's another approach for posterity: a small recursive method to disable the navigation controls:

private void disableNav(Container c) {
for (Component x : c.getComponents())
  if (x instanceof JComboBox)
    ((JComboBox)x).setEnabled(false);
  else if (x instanceof JButton) {
    String text = ((JButton)x).getText();
    if (text == null || text.isEmpty())
      ((JButton)x).setEnabled(false);
    }
  else if (x instanceof Container)
    disableNav((Container)x);
  }

Then call as follows:

JFileChooser fc = new JFileChooser(imgDir);
disableNav(fc);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Images", "jpg", "gif", "png");
fc.setFileFilter(filter);
...


来源:https://stackoverflow.com/questions/8926146/jfilechooser-want-to-lock-it-to-one-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!