0

So the problem is this. I have a GUI that opens a JFileChooser and the user selects a file, When hits OK a new Class is called (Amostra Sample = new Amostra(namefile), where namefile is absolute path) and its gonna pre-process some data from file and display it on the GUI. Then the user is going to insert some options to preform the rest of actions on the file and save it. The problem is, i can't have access to the object "Sample" created in JButton1, and since the processing is kind of time consuming, having access to the work already done would be the ideal option. Any hints?

public class FormatFiles extends javax.swing.JFrame {

    /** Creates new form FormatFiles */
    public FormatFiles() {
        initComponents();
    }



    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        JFileChooser chooser = new JFileChooser();//Inicia Caixa Dialog
        FileNameExtensionFilter filter = new FileNameExtensionFilter("SNLT logs", "csv");//Restringe tipo de ficheiros  
        chooser.addChoosableFileFilter(filter);//Adiciona filtro á Caixa
        chooser.setAcceptAllFileFilterUsed(false);
        int returnVal = chooser.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION)  {
            try {
                File ficheiro = chooser.getSelectedFile();
                jLabel3.setText(ficheiro.getAbsolutePath()); 
                String namefile = ficheiro.getAbsolutePath();
                File openAs = new File(namefile);
                FileReader in = null;
                //Opens File
                try {
                    in = new FileReader(openAs);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
                }
                //Load file into jTextArea1
                try {
                    jTextArea1.read(in, openAs.toString());
                } catch (IOException ex) {
                    Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
                }
                //Calls Amostra
                Amostra Sample = new Amostra(namefile);
                String segundos = Double.toString(Sample.getsec());
                jLabel7.setText(segundos);

            } catch (FileNotFoundException ex) {
                Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ParseException ex) {
                Logger.getLogger(FormatFiles.class.getName()).log(Level.SEVERE, null, ex);
            }
    }                                        
    }





    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String Accao = jTextField1.getText();
        JOptionPane.showMessageDialog(null,Accao);
        //Can't Access Sample from here...

    }                                        

    /**
     * @param args the command line arguments
     */



    public static void FormatFiles(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new FormatFiles().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration                   
}

2 Answers 2

1

Move the declaration of sample outside the method, so it'll be accessible by the whole class, also, change it to start with lower case:

public class FormatFiles extends javax.swing.JFrame {
     ....
    Amostra sample;
     ....
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         ....
         sample = new Amostra(namefile);
         ....
    }
    ......
}
Sign up to request clarification or add additional context in comments.

Comments

0

Can't you just declare it as an instance variable?

To conform to best practice, the instance variable should be private, and its name should conform to the standard Java naming conventions by starting with a lowercase letter; e.g.

private Amostra sample;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.