2014-07-19 81 views
0

您知道,我有按鈕可以使用JFileChooser選擇一個文件。這是我的代碼按鈕取消JFileChooser在點擊時仍然會加載文件

private void buttonFIleBrowserInPanelActionPerformed(java.awt.event.ActionEvent evt) {               
    try { 

     JFileChooser chooser = new JFileChooser(); 
     chooser.setCurrentDirectory(new java.io.File(".")); 
     chooser.setDialogTitle("Please, choose your main file ... "); 

     chooser.setFileFilter(new FileFilter() { 

      @Override 
      public boolean accept(File f) { 
       return f.getName().toLowerCase().endsWith(".java") || f.isDirectory(); 
      } 

      @Override 
      public String getDescription() { 
       return "Java File (*.java)"; 
      } 
     }); 
     chooser.setAcceptAllFileFilterUsed(true); 
     chooser.showOpenDialog(null); 

     File f = chooser.getCurrentDirectory(); 
     File ff = chooser.getSelectedFile(); 
     mainFile = ff; 
     if (ff != null) { 
      nameFile = ff.toString(); 

      File namaFilenya = new File(nameFile); 
      try { 
       FileReader readFile = new FileReader(namaFilenya); 
       String readFileToString = readFile.toString(); 

       try { 
        txtFile.read(readFile, null); // this is the textArea that show the contains of file 
        txtPathMainFile.setText(nameFile); //this is the textarea that show the path of file 

       } catch (IOException ex) { 
        System.out.println(ex); 
       } 

      } catch (FileNotFoundException ex) { 
       System.out.println(ex); 

      } 

     } 
    } catch (Exception e) { 
    } 
} 

當我在選配點擊文件之一,然後單擊確定,它的成功加載,我需要的一切。但是,在另一種情況下,當我點擊文件,但我決定選擇取消,該文件仍然加載在我的textArea。如何解決這個問題呢 ?

回答

4

檢查JFileChooser的回覆。

示例代碼:

int returnVal = chooser.showOpenDialog(parent); 
if(returnVal == JFileChooser.APPROVE_OPTION) { 
    System.out.println("You chose to open this file: " + 
     chooser.getSelectedFile().getName()); 
    // rest your code goes here 
} 

您可以檢查CANCEL_OPTION爲好。

有關使用JFileChooser的信息,請參閱The Java Tutorial中的How to Use File Choosers

+0

'chooser.showOpenDialog(父);'把這個改成'parent'很好。其餘的很好,但那部分真的跳到了我的身上。 :) –

+0

@AndrewThompson這是一個示例代碼,這是一個很好的做法,通過對話框的父母。 – Braj

+1

*「這是一個很好的做法,通過對話框的父母。」* +1我們同意。 –

0

你應該檢查文件是否被選中。即你必須在代碼中指定一些if/else條件。

if(fileSelected){ 

//load in text Area 
}else{ 
//nothing 
} 

應該有一些由JFileChooser實例返回的布爾值。

1

您應該檢查JFileChooser返回的值。看到我的回答

int rv = chooser.showOpenDialog(null); 
if (rv == JFileChooser.APPROVE_OPTION) { 
    File file= chooser.getSelectedFile(); 
+0

這裏的東西不是特別的。它應該是文件,當然。 –

+0

帶有文件的部分在我的回答中不是主要部分,並且毫無疑問,這是一個答案。 –

0

感謝所有的幫助例如,此代碼現在的工作,我若這樣的代碼

int returnVal = chooser.showOpenDialog(null); 
     if (returnVal == JFileChooser.APPROVE_OPTION) { 
      File f = chooser.getCurrentDirectory(); 
      File ff = chooser.getSelectedFile();// 
      mainFile = ff; 
      if (ff != null) { t 
       nameFile = ff.toString(); 

       File namaFilenya = new File(nameFile); 
       try { 
        FileReader readFile = new FileReader(namaFilenya); 
        try { 

         txtFile.read(readFile, null); 
         txtPathMainFile.setText(nameFile); 

        } catch (IOException ex) { 
         System.out.println(ex); 
        } 

       } catch (FileNotFoundException ex) { 
        System.out.println(ex); 

       } 

      } 

     } 

謝謝..

相關問題