Link to home
Start Free TrialLog in
Avatar of swp3h6
swp3h6

asked on

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?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

it should already, add \n to the edn of each line
If there are line breaks in the file, they should appear in the text area
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 :)
>>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
Avatar of swp3h6
swp3h6

ASKER

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.
If the original file has line breaks, use the read method as i mentioned
Avatar of swp3h6

ASKER

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.
>>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());
>>StringBuffer sb = new StringBuffer(ta.getDocument().getLength());

That would be better as

StringBuffer sb = new StringBuffer(ta.getDocument().getLength() + extraLengthDueToLineBreaks);
Avatar of swp3h6

ASKER

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?
>>and printed to a txt file

Why not add the line breaks at that point?

printWriter.println(string);
Avatar of swp3h6

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> would using printWriter make the difference?

yes you should use a Writer for writing text files.
>> 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.
Avatar of swp3h6

ASKER

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!
:-)
thats whjat i suggested in my first comment

> add \n to the edn of each line