2012-08-11 3 views
3

현재 아래 코드에 따라 JTextField를 사용하여 파일 위치를 입력하는 JFrame 응용 프로그램이 있습니다. 내가하고 싶은 무엇자바 스윙 - 파인더 텍스트 상자

txtSource = new JTextField(); 
    txtSource.setToolTipText("/location/of/file/test.txt"); 
    txtSource.setText("/location/of/file/test.txt"); 
    txtSource.setBounds(16, 122, 412, 29); 
    contentPane.add(txtSource); 
    txtSource.setColumns(10); 

는 사용자에 대한 디렉토리 검색은 로컬 컴퓨터에있는 파일의 위치를 ​​선택하고 해당 위치가 텍스트 상자에 채우는 것입니다 수 있습니다.

아래 정보가 JCHooser에서 발견되었지만 구현 방법에 대한 도움이 필요하시면 잘 모르겠습니다. 사전에

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(); 

덕분에

+0

http://jsfiddle.net/arunpjohny/k24471o6/1/ –

답변

4

사용

int option = fc.showOpenDialog(frame); 
    if (option == JFileChooser.APPROVE_OPTION) { 
      txtSource.setText(fc.getSelectedFile().getAbsolutePath()) 
    } 

선택한 파일 절대 파일 위치에 텍스트 필드를 채 웁니다

+3

사용자가 디렉토리 만 선택할 수 있도록하려면'JFileChooser.setFileSelectionMode'에'JFileChooser.DIRECTORIES_ONLY'를 제공해야합니다. 이렇게하면 선택자가 디렉토리 만 표시하게됩니다. (그게 당신이하려는 일이라면) – MadProgrammer

+0

잘 작동했습니다. 그걸 건배. – Yonkee