Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save JTextArea text to a txt file

I'm having trouble saving text from a JTextArea to a text file. When I save the data my text file has nothing in it. I feel like I'm writing to the output wrong. Is there a better way to code this? Thanks for the help!

The class for the program

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class SaveClass extends JPanel
{
    JPanel cards;
    private JPanel card1;
    private JTextArea textarea1;
    private JFileChooser fc;

    public SaveClass()
    {
        Font mono = new Font("Monospaced", Font.PLAIN, 12);

        textarea1 = new JTextArea();
        textarea1.setFont(mono);



        card1 = new JPanel();
        card1.add(textarea1);



        cards = new JPanel(new CardLayout());
        cards.add(card1, "1");

        add(cards, BorderLayout.CENTER);



        setBorder(BorderFactory.createTitledBorder("Text here"));
        setFont(mono);
    }

    public String getText1()
    {
        return this.textarea1.getText();
    }

    public void Save()
    {
        SaveClass sa = new SaveClass();
        String text = sa.getText1();

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory( new File( "./") );
        int actionDialog = chooser.showSaveDialog(this);
        if (actionDialog == JFileChooser.APPROVE_OPTION)
        {
            File fileName = new File(chooser.getSelectedFile( ) + "" );
            if(fileName == null)
                return;
            if(fileName.exists())
            {
                actionDialog = JOptionPane.showConfirmDialog(this,
                                   "Replace existing file?");
                if (actionDialog == JOptionPane.NO_OPTION)
                    return;
            }
            try
            {
                BufferedWriter out = new BufferedWriter(new FileWriter(fileName));

                    out.write(text);
                    out.close();
            }
            catch(Exception e)
            {
                 System.err.println("Error: " + e.getMessage());
            }
        }

    }
}

The Main program

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SaveMain extends JFrame
{

    private SaveClass canvas;

    private JPanel buttonPanel;
    private JButton btnOne;


    public SaveMain()
    {
        super("Save JTextArea text to a txt file");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        canvas = new SaveClass();

        buildButtonPanel();

        add(buttonPanel, BorderLayout.SOUTH);
        add(canvas, BorderLayout.CENTER);

        pack();
        setSize(800, 800);
        setVisible(true);

    }
    private void buildButtonPanel()
    {
        buttonPanel = new JPanel();

        btnOne = new JButton("Save");

        buttonPanel.add(btnOne);


        btnOne.addActionListener(new btnOneListener());


    }
    private class btnOneListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == btnOne)
            {
                canvas.Save();
            }
        }
    }


    public static void main(String[] args)
    {
        new SaveMain();
    }

}
like image 831
Brian P Avatar asked Dec 13 '22 03:12

Brian P


2 Answers

I think the problem is that you create an instance of SaveClass in the Main class, but in the Save method, that is in the SaveClass you create another instance, and you read the text from that instance. So you might want to do this to the Save() method:

delete the SaveClass sa = new SaveClass(); 

and then:

String text = this.getText1();
like image 146
Matei Suica Avatar answered Dec 25 '22 06:12

Matei Suica


        FileWriter pw = new FileWriter ("filename.txt");
        txtarea.write(pw); //Object of JTextArea

You need only two statements to write content of JTextArea into file...

I hope this will help you...

like image 25
Bhushankumar Lilapara Avatar answered Dec 25 '22 04:12

Bhushankumar Lilapara