我可以知道如何让JFileChooser只显示d:驱动器内容吗?我看不到任何公共方法让我这样做。
感谢。
答案 0 :(得分:2)
讨论here有一些例子。
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());
答案 1 :(得分:0)