JFileChooser,想把它锁定到一个目录 [英] JFileChooser, want to lock it to one directory

查看:63
本文介绍了JFileChooser,想把它锁定到一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个程序你可以下载文件,我希望JFileChooser被锁定到一个文件夹(目录),以便用户无法浏览其他任何内容。他只能选择文件夹中的文件C:\ Users \ Tommas \ Dropbox \ Prosjekt RMI \ SERVER \。我试过这么搜索但没找到任何东西。
我的代码是:

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

这个工作正常,但现在我可以转到Project RMI文件夹,我不知道我希望它能做到。

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

在此先感谢:)

编辑:我在你的帮助下做了什么:

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


推荐答案

自定义 FileSystemView ,将其用作 JFileChooser 构造函数接受FSV ..

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

这篇关于JFileChooser,想把它锁定到一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆