I am writing a program with java and i am having trouble displaying my .txt in my JTextArea.
it shows it as 1 appended line and i want it to display the way it is set out in my text file.
Here is my code:

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

public class FAQ extends JFrame implements ActionListener
{
    JMenu menu = new JMenu ("Menu");
    JMenuItem exitMenu = new JMenuItem ("Exit");
    JMenuItem homeMenu = new JMenuItem ("Home");

    JPanel button = new JPanel ();
    JPanel text = new JPanel ();

    JTextArea faqs = new JTextArea ();

    JButton back = new JButton ("Back");

    private FAQReader[] reader;

    Container cont;

    TwoOceans first = null;

    public FAQ () throws Exception
    {
        super ("FAQs");
        setSize (1000, 800);
        setResizable (true);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        cont = getContentPane ();
        cont.setLayout (new BorderLayout ());

        File fa = new File ("Docs/FAQs.txt");
        BufferedReader reader = null;
        try
        {
            reader = new BufferedReader (new FileReader (fa));
            String text = null;
            String savetext = null;
            while ((text = reader.readLine ()) != null)
            {
                savetext += text;
            }
            faqs.setText (savetext);
        }
        catch (Exception e)
        {
            System.out.println ("Error reading file");
            System.out.println (e.toString ());
        }

        JMenuBar mb = new JMenuBar ();
        setJMenuBar (mb);

        mb.add (menu);

        menu.add (exitMenu);
        menu.add (homeMenu);

        button.setLayout (new GridLayout (1, 5));
        button.add (back);

        back.addActionListener (this);

        cont.add (button, BorderLayout.SOUTH);
        cont.add (faqs);
        faqs.setEditable (false);

        exitMenu.addActionListener (this);
        homeMenu.addActionListener (this);

        faqs.setLineWrap(true);

        setVisible (true);
    }


    public void actionPerformed (ActionEvent event)
    {
        Object source = event.getSource ();

        if (source == exitMenu)
        {
            System.exit (0);
        }


        if (source == homeMenu)
        {
            first = new TwoOceans ();
            this.dispose ();
        }


        if (source == back)
        {
            first = new TwoOceans ();
            this.dispose ();
        }
    }


    public static void main (String[] args) throws Exception
    {
        new FAQ ();
    }
}

Recommended Answers

All 2 Replies

readLine drops the line termination character from the input stream, so all you have to do is put it back again, eg

savetext += text + "\n";

OMG it worked! thanks so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.