skydawg
skydawg

Reputation: 1

Trying to get BufferedWriter to save textarea to txt file

I am trying to build a calendar app and use a memo section that I labeled notes. I have an add button to add a new note and I want it to add to the current file in the path. I am trying to use a BufferWriter to do this. I have attached the newNote() method that opens a new frame and allows for the new text. I think I am trying to append the new text to the current file but the examples I have seen shows to do it this way. The output of the txt file is not what I expected. I think it is due to calling the textArea object and it is pulling the data of the object and not the input inside the textArea. I am somewhat new to Java and am doing this project for personal use, not a class. Any help and insight would be appreciated. This is also my first time posting in a forum so please let me know if there is a better way of doing this.

The newNote() method.

public static void newNote() {//opens new frame to create a new note
    //variables for the new window
    JFrame noteFrame = new JFrame("New Note");
    JPanel notePanel = new JPanel();
    JButton cancelButton = new JButton("Cancel");
    JButton addButton = new JButton("Add");
    JTextArea textArea = new JTextArea("Add notes here");
    //creates and positions buttons
    addButton.setBounds(150,330,65,40);
    addButton.addActionListener(new ActionListener() {//writes contents to a txt file when Add is clicked
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter((new FileWriter("/home/skydawg/pCloudDrive/Documents/test/Log.txt", true)));

                writer.write(String.valueOf(textArea));
                writer.newLine();
                writer.flush();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (writer != null) try {
                    writer.close();
                } catch (IOException ioe2) {
                    // just ignore it
                }
                noteFrame.dispose();//closes the frame
            }}
    });

The output to the txt file

newjavax.swing.JTextArea[,10,10,280x295,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@13e59af,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=218,g=218,b=218],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=javax.swing.plaf.ColorUIResource[r=255,g=255,b=255],selectionColor=javax.swing.plaf.ColorUIResource[r=134,g=108,b=186],colums=0,columWidth=0,rows=0,rowHeight=0,word=true,wrap=true]

Upvotes: 0

Views: 198

Answers (2)

The_Cute_Hedgehog
The_Cute_Hedgehog

Reputation: 1340

JTextArea extends JTextComponent. So you have a method called write(java.io.Writer writer).

That means, you can call textArea.write(writer) in your code.

Upvotes: 3

GhostCat
GhostCat

Reputation: 140417

Here:

writer.write(String.valueOf(textArea));

the javadoc for that method:

Returns the string representation of the Object argument.

That doesn't do what you expect it to do. That is like calling textArea.toString() ... which again: doesn't give you the string content of your textArea.

Instead, you want to call textArea.getText(), and write that to your file.

Beyond that: you should not throw so many things together. Meaning: write a utility class that just writes some text to a file. Test that separately. And only when that part works, you put your GUI around that. Your approach is: throwing everything together, testing all at once. Which means that you run from one problem to the next. Don't do that. Slice your problem into many small parts, and think hard "how can i solve and test each part in isolation"!

Finally, as you have just seen: GUI programming and GUI components are complicated. Trying to learn these things by trial and error is a really inefficient strategy! It is really better to read a good book or tutorial (see for example). Start with working examples, instead of pulling together 5 different things that are all new to you! And most importantly: each time you use a new class you haven't studied before: take the time to read the corresponding javadoc. Top to bottom! You can invest that time upfront, or you can spend 1 hour not understanding what is going on, to then invest that time.

Upvotes: 3

Related Questions