line breaks in a jtextarea

I am working with an application that imports entries from a text file into separate jtext areas.  The problem is that when I open the text file with this application (which I didn't write), It interprets each entry as one line, and the user has to manually expand the jtextarea, which causes exceptions to be thrown.  Is there a special character I can use to get the jtextarea to recognize the line breaks?
swp3h6Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
it should already, add \n to the edn of each line
0
CEHJCommented:
If there are line breaks in the file, they should appear in the text area
0
objectsCommented:
if you're reading the file line by line it will strip the new lines so you'll need to add them as I mentioned above :)
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

CEHJCommented:
>>if you're reading the file line by line

And if you are, and you only need to fill the text area, that's not an efficient way to do it anyway. Use the read method of the text area
0
swp3h6Author Commented:
yes, thats the problem, each entry is just one long string, and I want to insert line breaks.  I've tried adding "\n" and a few other characters, but they don't insert line breaks, they just show up in the text.
0
CEHJCommented:
If the original file has line breaks, use the read method as i mentioned
0
swp3h6Author Commented:
I'm pretty sure it uses the read method, but i'm not sure.  as mentioned above, I didn't write the part thats reading the files in.  And I already know that the original file has no line breaks.  I just want to add them.
0
CEHJCommented:
>>already know that the original file has no line breaks.

That's different then

StringBuffer sb = new StringBuffer(ta.getDocument().getLength());
sb.append(sb.toString());

Now insert them where you want in sb, then

ta.setText(sb.toString());
0
CEHJCommented:
>>StringBuffer sb = new StringBuffer(ta.getDocument().getLength());

That would be better as

StringBuffer sb = new StringBuffer(ta.getDocument().getLength() + extraLengthDueToLineBreaks);
0
swp3h6Author Commented:
I'm not sure I understand.  I did write the method that generates the text file to be read (thats how I know for sure there are no linebreaks).  Each entry is a single string with no line breaks, read from an array, and printed to a txt file.  is there a way to insert these line breaks as the file is generated?
0
CEHJCommented:
>>and printed to a txt file

Why not add the line breaks at that point?

printWriter.println(string);
0
swp3h6Author Commented:
Thats what I have in mind, but I tried manually inserting "\n" into the txt file and it doesn't work.... .  i'm using a printStream to write the file... would using printWriter make the difference?
0
CEHJCommented:
It's easier to do so and that would put the correct line breaks in
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mr_egyptianCommented:
Not terribly pretty, but maybe this works for you?
If you load your string (no newlines) into a String, say str, and want to break every, say 10 words.

--------------------------------------------------------------------
      static String breakString(String line, int len){
            String out="";
            String[] words=line.split(" ");
            
            for(int i=0;i<words.length;i=i+len){
                  for(int j=i;(j<i+len && j<words.length);j++){
                        out=out+words[j]+" ";
                  }
                  out=out+"\n";
            }
            return out;
      }

      str=breakString(str,10);
--------------------------------------------------------------------
0
objectsCommented:
> would using printWriter make the difference?

yes you should use a Writer for writing text files.
0
Mayank SAssociate Director - Product EngineeringCommented:
>> would using printWriter make the difference

Yes, you should use the println () method of the writer.

>> but I tried manually inserting "\n"

Maybe you should have tried "\r\n" - the new-line characters on all environments differ so its better to use the Writer API.
0
swp3h6Author Commented:
problem solved.  I split the strings up in 60 character increments, and used printWriter to print each one of the increments to the file.  Thanks all!
0
CEHJCommented:
:-)
0
objectsCommented:
thats whjat i suggested in my first comment

> add \n to the edn of each line
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.