1

I am creating a GUI using methods and classes, and am trying to create a button where the user can select it and then choose a file. The file will then read in what is stored in the file into a textarea/textfield/JLabel. Can anyone help me with my code, I'm not sure how to code it so I can select the button to choose a file and it then reads the text into the GUI.

public void addLoadFile(){
        btnLoadFile = new JButton("Load file of Books");
        btnLoadFile.setBounds(415, 30, 150, 20);
        btnLoadFile.addActionListener(this);
        panelLoadFile.add(btnLoadFile); 

        textArea = new JTextArea();


    }

    class this implements ActionListener {
        @Override
        JFileChooser fileChooser = new JFileChooser();
        int ret = fileChooser.showOpenDialog(this);
        if(ret== JFileChooser.APPROVE_OPTION)
        {
            File f= fileChooser.getSelectedFile();
            try
            {
                FileReader r=new FileReader(f);
                area.read(4,"");
            }
            catch (Exception ee)
        }
    System.out.prinkln("");
    }
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Sophie
  • 29
  • 1
  • 7
  • 1
    Possible duplicate of [How do I create a Java string from the contents of a file?](http://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file) – VGR Aug 09 '16 at 21:31

1 Answers1

1

The following snippet might help:

Add Listener to Button

 jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

Code for the Action Performed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFileChooser fileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Text Files(*.txt)", "txt");
    fileChooser.setFileFilter(filter);
    fileChooser.setCurrentDirectory(new File(System
            .getProperty("user.home")));
    int result = fileChooser.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(selectedFile));
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            String all = sb.toString();
            jTextArea1.setText(all);
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
  • Try to run in in a background thread, e.g. with `SwingWorker` or `ExecutorService` so the UI won't hang with large files. – Tamas Rev Aug 09 '16 at 14:23