2

if I enter text in the JTextArea and click the "Save" button, the JTextArea text should write/save into a .txt file. Is my try & catch in the correct place being in the event handler method or should parts of it be in the constructor?

This is my code:

package exercises;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class SimpleNotePadApp extends JFrame implements ActionListener {

JButton button1 = new JButton("Open");
JButton button2 = new JButton("Save");

public SimpleNotePadApp(String title) {
    super(title);                             
    setDefaultCloseOperation(EXIT_ON_CLOSE);  
    setSize(300, 350);                        
    setLayout(null);


    JTextArea newItemArea = new JTextArea();
    newItemArea.setLocation(3, 3);
    newItemArea.setSize(297, 282);
    getContentPane().add(newItemArea);

    button1.setLocation(30,290);  
    button1.setSize(120, 25);
    getContentPane().add(button1);

    button2.setLocation(150,290);  
    button2.setSize(120, 25);
    getContentPane().add(button2);

}

public static void main(String[] args) {
    SimpleNotePadApp frame;

    frame = new SimpleNotePadApp("Text File GUI");      
    frame.setVisible(true);                             
}

public void actionPerformed(ActionEvent e) {

    if(e.getSource() == button1)
    {
        try {
            PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt"));
            newItemArea.getText();
            newItemArea.write(out);
            out.println(newItemArea);
            out.flush();
            out.close();

        } catch (IOException e1) {
            System.err.println("Error occurred");
            e1.printStackTrace();
        }
    }
}
}

Thanks in advance

Caiz
  • 49
  • 1
  • 2
  • 7
  • 3
    `Is my try & catch in the correct place...` - Why would it be in the constructor? Do you execute your code from the constructor? Also, use the JTextArea.write(...) method to save the data to a file. – camickr Sep 27 '17 at 02:06
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 27 '17 at 02:41

1 Answers1

2

Your try ... catch is in the correct spot, but the contents should just be:

        PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt"));
        newItemArea.write(out);
        out.close();

Consider using try-with-resources, and the .close() becomes unnecessary:

    try ( PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt")) {
        newItemArea.write(out);
    } catch (IOException e1) {
        System.err.println("Error occurred");
        e1.printStackTrace();
    }

Also, you'll need to attach the ActionListener to the JButton during the construction:

button2.addActionListener(this);

(this is the SimpleNotePadApp instance, which implements ActionListener)

Finally, you'll want:

 if(e.getSource() == button2)

... since button2 is your "Save" button (not button1)

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44
  • Thanks for your help! However, for some reason it's still not saving into a text file. It's definitely made it through to the "try" part since I've tested it by printing something to the console... – Caiz Sep 27 '17 at 02:29
  • Are you sure it's not writing into the text file? I'd recommend doing some changes to the code in the question for debugging purposes. E.G. change `PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt"));` to `File f = new File("TestFile.txt"); PrintWriter out = new PrintWriter(new FileWriter(f));` then later (after the save), do `Desktop.getDesktop().open(f);` – Andrew Thompson Sep 27 '17 at 02:44