2013-05-14 3 views
-1

.txt 파일을 검색하는 프로그램을 만들었습니다.'열기'대화 상자를 만드는 방법은 무엇입니까?

파일을 클릭하면 "연결 프로그램"대화 상자가 나타나야하며 대화 상자에 설치된 모든 프로그램 목록이 포함된다는 것을 의미합니다. 내가 파일을 통해 검색을 위해이 코드를 사용하고

:

public File[] finder(String dirName) 
    { 
     // Create a file object on the directory. 
     File dir = new File(dirName); 
     // Return a list of all files in the directory. 
     return dir.listFiles(new FilenameFilter(); 
    } 

    public boolean accept(File dir, String filename) 
    { 
     return filename.endsWith(".txt"); 
    } 

어떤 자바 코드를 걸하는 데 사용할 수있는 대화 상자 "열기"는 표시?

+0

자바를 통해 레지스트리를 액세서하는 방법에 대해 확인이 링크를 참조? 이것은 Google의 첫 번째 결과입니다. http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html – jazzbassrob

+0

가능한 [JFileChooser Filters] 중복 (http://stackoverflow.com/questions/13517770/) jfilechooser-filters) –

+0

내 편집을 확인하십시오. 몇 가지 신중한 독서 후 나는 이것이 OP가 원하는 것이라고 생각합니다. – christopher

답변

3

이 경우 FileChooser을 사용해야합니다.

//Create a file chooser 
final JFileChooser fc = new JFileChooser(); 
... 
//In response to a button click: 
int returnVal = fc.showOpenDialog(aComponent); 


public void actionPerformed(ActionEvent e) { 
    //Handle open button action. 
    if (e.getSource() == openButton) { 
     int returnVal = fc.showOpenDialog(FileChooserDemo.this); 

     if (returnVal == JFileChooser.APPROVE_OPTION) { 
      File file = fc.getSelectedFile(); 
      //This is where a real application would open the file. 
      log.append("Opening: " + file.getName() + "." + newline); 
     } else { 
      log.append("Open command cancelled by user." + newline); 
     } 
    } ... 
} 
+0

파일 선택기를 원하지 않습니다. 대화 상자에는 OS에만 설치된 프로그램이 포함되어 있습니다. – Ranjith

+0

@ user2364985 ExtensionFilter를 사용하여 FileChooser를 구성 할 수 있습니다. http://docs.oracle .com/javase/7/docs/api/javax/swing/filechooser/FileNameExtensionFilter.html – Vairis

2

내가 만드는 데 사용할 수있는 어떤 자바 코드 대화 상자 "열기"가 나타납니다 살펴 here을 가지고?

내 지식으로는 J2SE에는 아무 것도 없습니다. OTOH Desktop API는 어떤 앱이든 File을 열 수 있습니다. 기본값은입니다.

관련 문제