简体   繁体   中英

Send the file.getAbsolutePath() from JFileChooser as a parameter to another button

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM