Search code examples
javaswingnetbeans-7java-6

Load a text file in a JTextArea (java) in a different class


I have a problem with a project in netbeans: I have mainly to class:

CLASS MainFrame

  ...  
    Model m = null;
    File f;
    String filename = "";
    String specific = readSpecification();
    private void openModelActionPerformed(java.awt.event.ActionEvent evt) {                                          
    // TODO add your handling code here:
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
        chooser.setFileFilter(filter);
        chooser.showOpenDialog(null);
        f = chooser.getSelectedFile();
        filename = f.getAbsolutePath();
        PrincipalFrame prFrame1 = new PrincipalFrame();

        prFrame1.setVisible(true);


    }                                         



        public  String readSpecification() {

            String spec = "";
             /**
         * Reads the model specification from file.
         * 
         * @return a <code>String</code> with the model specification
         */
            try {
                BufferedReader reader = new BufferedReader(new FileReader(filename));
                String line = reader.readLine();
                while(line!=null) {
                    spec += line + "\n";
                    line = reader.readLine();
                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return spec;
        }

    }

The class PrincipalFrame is mostly empty.

The MainFrame Class should select a file.txt to be open. The PrincipalFrame Class, has a JTextArea that should be fulfilled with the txt choosen by the MainFrame Class. In other words, in a first moment the MainFrame is opened, and the user should select a txt file. Once he chooses it, the PrincipalFrame appears and its JTextArea should be alreay fulfilled with the file.txt. Hope this is clear now! Thanks for your help!


Solution

  • You could create a setSpecification method in the PrincipalFrame class that fills the JTextArea. This way you can pass the specification text from the MainFrame to the PrincipalFrame class. For example:

    MainFrame.java:

    public class MainFrame {
        // [...]
    
        private void openModelActionPerformed(java.awt.event.ActionEvent evt) {
            // [...]
            filename = f.getAbsolutePath();
            PrincipalFrame prFrame1 = new PrincipalFrame();
            prFrame1.setSpecification(readSpecification());
            prFrame1.setVisible(true);
        }
    
        // [...]
    }
    

    PrincipalFrame.java:

    public class PrincipalFrame {
        private JTextArea textArea;
    
        // [...]
    
        public void setSpecification(String specification) {
            textArea.setText(specification);
        }
    }