사용자를 디렉토리와 하위 디렉토리로 제한하려고하지만 "상위 디렉토리"버튼을 사용하여 임의의 디렉토리를 탐색 할 수 있습니다.JFileChooser를 디렉토리로 어떻게 제한합니까?
어떻게해야합니까?
사용자를 디렉토리와 하위 디렉토리로 제한하려고하지만 "상위 디렉토리"버튼을 사용하여 임의의 디렉토리를 탐색 할 수 있습니다.JFileChooser를 디렉토리로 어떻게 제한합니까?
어떻게해야합니까?
직접 설정할 수 있습니다. FileSystemView.
class DirectoryRestrictedFileSystemView extends FileSystemView
{
private final File[] rootDirectories;
DirectoryRestrictedFileSystemView(File rootDirectory)
{
this.rootDirectories = new File[] {rootDirectory};
}
DirectoryRestrictedFileSystemView(File[] rootDirectories)
{
this.rootDirectories = rootDirectories;
}
@Override
public File createNewFolder(File containingDir) throws IOException
{
throw new UnsupportedOperationException("Unable to create directory");
}
@Override
public File[] getRoots()
{
return rootDirectories;
}
@Override
public boolean isRoot(File file)
{
for (File root : rootDirectories) {
if (root.equals(file)) {
return true;
}
}
return false;
}
}
당신은 분명히 더 나은 "createNewFolder"방법을 확인해야합니다, 그러나 이것은 더 디렉토리 중 하나에 사용자를 제한하지 :
Incase의 다른 사람이 앞으로이 필요합니다.
그리고는 다음과 같이 사용 :
FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File("X:\\"));
JFileChooser fileChooser = new JFileChooser(fsv);
또는 같은
: 이FileSystemView fsv = new DirectoryRestrictedFileSystemView(new File[] {
new File("X:\\"),
new File("Y:\\")
});
JFileChooser fileChooser = new JFileChooser(fsv);
@Allain –
Allain이의 솔루션은 거의 완료되었습니다. 세 가지 문제를 해결하기 위해 열려 있습니다 :
public TFile getHomeDirectory() { return rootDirectories[0]; }
세트 클래스와 생성자 public
변경 JFileChooser fileChooser = new JFileChooser(fsv);
JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory(),fsv);
에 나는 TrueZips TFileChooser과를 통해 압축 파일에 머물 사용자를 제한하기 위해 사용 위의 코드를 약간 수정하면 완벽하게 작동합니다. 고마워.
복잡 할 필요가 없습니다. 당신은 쉽게 당신은 기본 FileSystemView가받을 수 있나요 (예를 들어,이 위임하는) 방법을 여기 How to Use File Choosers
이것은 일반적으로 디렉토리로 제한되지만 OP의 질문 인 특정 디렉토리로는 제한되지 않습니다. –
이상의 참조를 읽을 수있는이
같은 JFileChooser를의 선택 모드를 설정할 수 있습니까? –
@Jason S - 아마도 정적 메서드'getFileSystemView() '를 통해 – McDowell
누군가가 그것을 필요로 할 수 있다면, 다음은 OP가 원하는 것의 정확한 예입니다 : http://tips4java.wordpress.com/2009/01/28/single -root-file-chooser/ –