简体   繁体   中英

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();
    }

}
        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...

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();

It seems to me that you are never adding your JTextArea to the JFrame. More specifically, you add your JTextArea called textarea1 to a JPanel called card1 but this JPanel is never added to the JFrame.

EDIT: Oh I see that you are adding the SaveClass , which is a JPanel to the frame (although there are many, many JPanels that seemingly have nothing in them in this SaveClass). However, when you go to call the Save() method, you make a brand new SaveClass. In this new SaveClass the JTextArea is initialized with nothing in it to it doesn't write anything to the file.

你可能需要在out.close()之前使用out.flush()。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM