0

I have a small problem with a Java app. I'm using a JFileChooser to open up a text file, display its content on a textzone, while keeping it ready for some other actions.

One of these actions are calling a method when pressing an 'analyse' button in my GUI. My problem is that it seems like I can't change the header of the action listeners on the JFrame class, in order to put the file.getAbsolutePath() as an out from the JFileChooser to the buttons action listener.

Below is the code I have. Basically I want the "test.txt" to be replaced with the file I open in the JFileChooser.

How do I solve this problem ?

  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:
}                                       

edit : I solved the issue. All I had to do was to use the fileChooser.getSelectedFile() everytime I needed to use the file elsewhere.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Majd
  • 3
  • 3
  • 1
    Post some code about your problem. The description is too vague. – Mubin Aug 15 '16 at 09:29
  • 1
    Use `Action`, seen [here](http://stackoverflow.com/a/4039359/230513), to encapsulate the `File`. – trashgod Aug 15 '16 at 12:17
  • *"edit : I solved the issue."* Congrats on solving the problem. :) Don't enter answers into the question. Either create your own answer below, or delete the question. – Andrew Thompson Aug 17 '16 at 11:46

1 Answers1

0

I solved the issue. All I had to do was to use the fileChooser.getSelectedFile() everytime I needed to use the file elsewhere.

Majd
  • 3
  • 3