2008-11-12 3 views

답변

37

사전에

덕분에, this example을 확인합니다. 저장 -로 다음

final JFileChooser fc = new JFileChooser(); 
fc.showOpenDialog(this); 

try { 
    // Open an input stream 
    Scanner reader = new Scanner(fc.getSelectedFile()); 
} 
+27

JFileChooser의 모든 유연성을 필요로하지 않는다면 대신 java.awt.FileDialog를 사용해야합니다. OS X 사용자가 감사 할 것입니다. FileDialog는 네이티브 파일 선택 창을 사용하는 반면 JFileChooser는 스윙 구성 요소이며 키보드 단축키 및 기타 정보가 부족합니다. –

+6

Windows 사용자도 감사합니다! –

12

나는 내가 필요 정확히 한 코드의 빠른 조각을 사용하여 종료 파일 대화 상자 : 여기

String filename = File.separator+"tmp"; 
JFileChooser fc = new JFileChooser(new File(filename)); 

// Show open dialog; this method does not return until the dialog is closed 
fc.showOpenDialog(frame); 
File selFile = fc.getSelectedFile(); 

// Show save dialog; this method does not return until the dialog is closed 
fc.showSaveDialog(frame); 
selFile = fc.getSelectedFile(); 

만들고 쇼 파일 선택 대화 상자 두 개의 버튼을 생성하는보다 정교한 예이다.

// This action creates and shows a modal open-file dialog. 
public class OpenFileAction extends AbstractAction { 
    JFrame frame; 
    JFileChooser chooser; 

    OpenFileAction(JFrame frame, JFileChooser chooser) { 
     super("Open..."); 
     this.chooser = chooser; 
     this.frame = frame; 
    } 

    public void actionPerformed(ActionEvent evt) { 
     // Show dialog; this method does not return until dialog is closed 
     chooser.showOpenDialog(frame); 

     // Get the selected file 
     File file = chooser.getSelectedFile(); 
    } 
}; 

// This action creates and shows a modal save-file dialog. 
public class SaveFileAction extends AbstractAction { 
    JFileChooser chooser; 
    JFrame frame; 

    SaveFileAction(JFrame frame, JFileChooser chooser) { 
     super("Save As..."); 
     this.chooser = chooser; 
     this.frame = frame; 
    } 

    public void actionPerformed(ActionEvent evt) { 
     // Show dialog; this method does not return until dialog is closed 
     chooser.showSaveDialog(frame); 

     // Get the selected file 
     File file = chooser.getSelectedFile(); 
    } 
}; 
6

다음 예는 파일 선택기를 만들고 그것뿐만 먼저 파일 열기 대화 상자를 표시하고 :

0

WebStart와 새로운 6u10 PlugIn에서 보안 권한 없이도 FileOpenService를 사용할 수 있습니다. 분명한 이유로 파일 경로가 아닌 파일 내용 만 가져옵니다.

관련 문제