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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
CEHJConnect With a Mentor Commented:
It's easier to do so and that would put the correct line breaks in
0
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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
 
mr_egyptianConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.