Question

I want to read a file using jFileChooser. jFileChooser will come up after press of a button (say jbutton1ChooseFile) and select the required file. After the selection is complete, another button (say jbutton2) will be used to read the contents of the file which has just been selected by the user. So on clicking on jbutton2, selected file will be read.

I am posting few lines of code so that it would be easy to understand what I mean to say:

     private void jButton1ChooseFileChooseFileActionPerformed(java.awt.event.ActionEvent evt) {                                                             
    // TODO add your handling code here:
    JFileChooser loadFile= new JFileChooser();
    loadFile.setApproveButtonText("Select File");
    loadFile.setAcceptAllFileFilterUsed(false);
    FileNameExtensionFilter f1 = new FileNameExtensionFilter("Text Files", "txt", "text","rtf","doc","docx");
    loadFile.setFileFilter(f1);
    switch (loadFile.showOpenDialog(EncDecApp.this))
             {
                case JFileChooser.APPROVE_OPTION:

                    JOptionPane.showMessageDialog(EncDecApp.this, "Selection Successfull!",
                                                 "Attention!",
                                                 JOptionPane.OK_OPTION);

                   jButton1ChooseFile.setText("File Chosen");
                   jLabelChooseFile.setText(String.valueOf(loadFile.getSelectedFile()).substring(0,30)+"...");
                   fileSelect=true;
                   break;

                case JFileChooser.CANCEL_OPTION:
                   JOptionPane.showMessageDialog(EncDecApp.this, "No file chosen",
                                                 "Attention!",
                                                 JOptionPane.OK_OPTION);
                   loadFile.setSelectedFile(null);
                   jButton1ChooseFile.setText("Browse..");
                   jLabelChooseFile.setText("Choose file to encrypt");
                   break;

                case JFileChooser.ERROR_OPTION:
                   JOptionPane.showMessageDialog(EncDecApp.this, "Error",
                                                 "Choosing File",
                                                 JOptionPane.OK_OPTION);
                   loadFile.setSelectedFile(null);
                   jButton1ChooseFile.setText("Browse..");
                   jLabelChooseFile.setText("Choose file to encrypt");
             }
    loadFile.setVisible(true);
}                               

Upto this it's working perfectly. Now, the code for jButton2 is as follows:

        private void jButton2EncryptEncryptActionPerformed(java.awt.event.ActionEvent evt) {                                                       
    // TODO add your handling code here:
    //Charset charset=Charset.forName("UTF-8");
    int returnVal=loadFile.showOpenDialog(jLabel1);
    if(returnVal==loadFile.APPROVE_OPTION)
    {
      File filePath = loadFile.getSelectedFile();    

    try{
        BufferedReader in = new BufferedReader(new FileReader(filePath));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
            jTextArea1.append(line + "\n");
        }
    in.close();
    }
    catch(IOException ex)
    {
           System.err.println("Open plaintext error: "+ex);
    }     
    }
}                  

Any help will be highly appreciated.

Était-ce utile?

La solution

At first glance the problem appears to be that you are using a local variable for the JFileChooser. That is to say, you have the line:

JFileChooser loadFile= new JFileChooser();

In your jButton1ChooseFileChooseFileActionPerformed function, and yet also try to refer to loadFile in your jButton2EncryptEncryptActionPerformed function.

In order to have the loadFile object available to both you need to have said loadFile object be a member of the class to which both functions belong.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top