开发者

JFileChooser shows only d: drive content

May I know how I can mak开发者_如何学Ce JFileChooser to show only d: drive content? I cannot see any of its public method enables me to do so.

Thanks.


There is a discussion here with some examples.

class RestrictedWinFileSystemView extends FileSystemView{
    private File[] allowed = null;

    public RestrictedWinFileSystemView(){

    }

    public RestrictedWinFileSystemView(File[] allowed){
        this.allowed = allowed;
    }


    // apply filter here 
    public File[] getRoots(){
        if(allowed != null){
            return allowed;
        }

        File[] files = super.getRoots();
        java.util.List allow = new ArrayList();
        for(int i=0; i<files.length; i++){
            File desktop = files[i];
            File[] roots = desktop.listFiles();
            int rl = roots.length;
            for(int j=0; j<rl; j++){
                File cr = roots[j];
                File[] sroots = cr.listFiles();
                if(sroots != null){
                    for(int k=0; k<sroots.length; k++){
                        File cr1 = sroots[k];
                        String path = cr1.getAbsolutePath();
                        if(path.equals("D:\\"){
                            allow.add(cr1);
                        }
                    }
                }
            }
        }
        allowed = (File[])allow.toArray(new File[0]);
        return allowed;
    }

    public File createNewFolder(File dir){
        return null;
    }

    public boolean isHiddenFile(File f) {
        return super.isHiddenFile(f);
    }

    public boolean isRoot(File f){
        return super.isRoot(f);
    }
}


JFileChooser fc = new JFileChooser(new RestrictedWinFileSystemView());


Single Root File Chooser

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜