2008-08-28 9 views

답변

10

직접 설정할 수 있습니다. 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"방법을 확인해야합니다, 그러나 이것은 더 디렉토리 중 하나에 사용자를 제한하지 :

+0

이상의 참조를 읽을 수있는이

JFileChooser fc = new JFileChooser(); fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fc.setMultiSelectionEnabled(false); 

같은 JFileChooser를의 선택 모드를 설정할 수 있습니까? –

+1

@Jason S - 아마도 정적 메서드'getFileSystemView() '를 통해 – McDowell

+0

누군가가 그것을 필요로 할 수 있다면, 다음은 OP가 원하는 것의 정확한 예입니다 : http://tips4java.wordpress.com/2009/01/28/single -root-file-chooser/ –

20

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); 
+0

@Allain –

5

Allain이의 솔루션은 거의 완료되었습니다. 세 가지 문제를 해결하기 위해 열려 있습니다 :

  • 시작 지점이 루트가 아닌 '홈'- 버튼을 클릭

    1. 이 제한에서 사용자 차기
    2. DirectoryRestrictedFileSystemView 패키지 외부에 액세스 할 수 없습니다

    1. @Override to DirectoryRestrictedFileSystemView

    public TFile getHomeDirectory() { return rootDirectories[0]; }

    1. 세트 클래스와 생성자 public

    2. 변경 JFileChooser fileChooser = new JFileChooser(fsv);JFileChooser fileChooser = new JFileChooser(fsv.getHomeDirectory(),fsv);

    에 나는 TrueZips TFileChooser과를 통해 압축 파일에 머물 사용자를 제한하기 위해 사용 위의 코드를 약간 수정하면 완벽하게 작동합니다. 고마워.

  • -1

    복잡 할 필요가 없습니다. 당신은 쉽게 당신은 기본 FileSystemView가받을 수 있나요 (예를 들어,이 위임하는) 방법을 여기 How to Use File Choosers

    +1

    이것은 일반적으로 디렉토리로 제한되지만 OP의 질문 인 특정 디렉토리로는 제한되지 않습니다. –

    관련 문제