2016-08-15 39 views
0

我對Java應用程序有一個小問題。我正在使用JFileChooser來打開文本文件,將其內容顯示在文本區域中,同時保留它用於其他一些操作。將JFileChooser中的file.getAbsolutePath()作爲參數發送到另一個按鈕

其中一個操作是在我的GUI中按下'分析'按鈕時調用方法。我的問題是,似乎我無法更改JFrame類上的動作偵聽器的標頭,以便將file.getAbsolutePath()JFileChooser中移出到按鈕動作偵聽器。

以下是我的代碼。基本上我想將「test.txt」替換爲我在JFileChooser中打開的文件。

我該如何解決這個問題?

private void OuvrirActionPerformed(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    int returnVal = fileChooser.showOpenDialog(this); 
    if (returnVal == JFileChooser.APPROVE_OPTION) { 
     File file = fileChooser.getSelectedFile(); 
     try { 
      // What to do with the file, e.g. display it in a TextArea 
      textarea.read(new FileReader(file.getAbsolutePath()), null); 
     } catch (IOException ex) { 
      System.out.println("problem accessing file" + file.getAbsolutePath()); 
     } 
    } else { 
     System.out.println("File access cancelled by user."); 
    } 
}          

private void FermerActionPerformed(java.awt.event.ActionEvent evt) {          
    System.exit(0);   // TODO add your handling code here: 
}          

private void traiterActionPerformed(java.awt.event.ActionEvent evt) {           
    Reader readme = new Reader(); 
    ArrayList<String[]> Noms = new ArrayList<String[]>(); 
    readme.file2string("test.txt", Noms); 
    // TODO add your handling code here: 
}          

編輯:我解決了這個問題。我所要做的就是每次需要使用fileChooser.getSelectedFile()來在別處使用該文件。

+1

發佈一些關於您的問題的代碼。描述過於模糊。 – Mubin

+1

使用'Action',見[這裏](http://stackoverflow.com/a/4039359/230513)封裝'File'。 – trashgod

+0

*「編輯:我解決了這個問題。」*恭喜你解決了這個問題。 :)不要在問題中輸入答案。請在下面創建自己的答案,或者刪除問題。 –

回答

0

我解決了這個問題。我所要做的就是每次需要使用fileChooser.getSelectedFile()來在別處使用該文件。

相關問題