Garmaine Staff asked 1 year ago

I can successfully write from text area to file, but am having trouble writing from file to text area. The problem seems to lie in Window.setJTextArea(). I have tried using different types of I/O options but am at an impasse. I'm out of my depth here, please offer guidance. There's more body text in class Window but it wasnt passing this site's review stage so I deleted it.

class Window {
        file = new File("/Users/samuelballard/eclipse-workspace/cse_1322/ZModule_6/src/lab12/WriteFile.java");
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public File getFile() {
        return file;
    }
    public void setEmpty() {
        textArea = new JTextArea("Empty");
    }
    public void setJTextArea() {
        textArea = new JTextArea("");
        try {
            reader = new FileReader("/Users/samuelballard/eclipse-workspace/cse_1322/ZModule_6/src/lab12/WriteFile.java");
            textArea.read(reader, "File");
            reader.close();
        } catch(IOException i) {
            i.printStackTrace();
        }
    }
    public void saveJTextArea() {
        try {
            writer = new FileWriter("/Users/samuelballard/eclipse-workspace/cse_1322/ZModule_6/src/lab12/WriteFile.java");
            textArea.write(writer);
            writer.close();
        } catch (IOException i) {
            i.printStackTrace();
        }
    }
}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

final class EventHandler extends Window {
    EventHandler() {
        put();
        get();
    }
    public void put() {
        put.addActionListener(new ActionListener() {
            // save text area to file
            public void actionPerformed(ActionEvent e) {
                saveJTextArea();
            }

        });
    }
    public void get() {
        get.addActionListener(new ActionListener() {
            // Display Contents of file
            public void actionPerformed(ActionEvent e) {
                setJTextArea();
            }

        });
    }
}