aspose file tools*
The moose likes Java in General and the fly likes Reading Text From a JtexArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading Text From a JtexArea" Watch "Reading Text From a JtexArea" New topic
Author

Reading Text From a JtexArea

Carlos Valderrama
Greenhorn

Joined: Feb 20, 2013
Posts: 14
Hi friends, I hope you're all fine!

This is my first post, and I haven't contributed to the community yet but, I would be very glad if any of you Java experts, could help me with the following issue which has been driving me crazy this week.

The thing is that i'm querying a field that returns a very long String and then displaying it into a jTextarea that is line-wrapped.

The text gets correctly displayed, I mean the line breaks are shown.

After that, I need to update that text into the database splitted in lines, there's where my problem is, the jTextArea.getText() metod returns the original string which has no "\n".

Making it impossible to split,

Here are some of the solutions that i have tried:

a) String[] f=jTextArea.getText().split("\r?\n|\r"); (with a very long list of combinations for the line separators)

b) the method getLineEndOffset() hasn't work for me cause I have said the String ain't detected separated by lines but just as a very long string.

c) stringTokenizer - failed.

d) System. getProperty("line.separator") has failed also.

what other alternative do I have?

Thanks in advance for any of your comments guys.











Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

Line wrapping in the JTextArea does not modify the string by adding line breaks, it simply displays the (unbroken) string across multiple lines. If you want to split the text into lines, and the text does not have line breaks in it to begin with, then you will need to add your own line breaks.

I haven't done this before but my strategy would be:
1: Define the number of characters per line
2: Offset from the start of the string by the number of characters per line.
3: Search backwards from that point for good places to split (space, tab, -, etc...)
4: Insert a line break
5: Memorize the position you are at (after the line break)
6: Repeat steps 1-5, starting at the position memorized in step 5, and stopping when you have fewer characters left then the number of characters per line.


Steve
Carlos Valderrama
Greenhorn

Joined: Feb 20, 2013
Posts: 14
Hi Steve! and thanks for taking the time to response me.

Ok I'm giving it a try to the algorithym that you proposed.

I'll let you know !


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Welcome to the Ranch

Remember that Strings are immutable. Put the String into a StringBuilder, which is specially designed for changes like that.
Carlos Valderrama
Greenhorn

Joined: Feb 20, 2013
Posts: 14
Hi guys! and thank you for welcoming me at the ranch!

I finally got it working, it cost me a lot of time but i made it following the recommendation given by Steve

here's the code

I hope it helps if anyone else needs it




Being everytime "cadena" gets printed the content of the lines of the jTextArea.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3932
    
  17

That is great that you got it to work, Congrats! A few items to consider for future development and improvement:

1- when posting here, please UseCodeTags (<- link). Makes the code a lot easier to read, don't you think (I added them to your post for you)

2- lines 17-20 are a bit redundant. There is no need to treat aub == 0 as a special case, since aub = aub + ub would provide the same results. So those 4 lines could be done in 1 line, which is simpler, and simpler is always better.

3- Consider getting the String from the JTextArea just once and storing it locally. Maybe add Sting sourceString = jTextArea4.getText(); at the start of the code, then use sourceString wherever you had been using jTextArea4.getText(). This is more expressive (as long as you name the String variable something useful), means fewer method calls, lower chance of a mistype causing an error (like typing jTextArea3.getText()). You can still mistype sourceString but at least the compiler will find the error for you (rather than accidentally getting text from the wrong jTextArea and weird results happening). Finally, it protects you from a certain amount of danger if the value in jTextArea4 is changed while you are doing your work.

4- Your code here (which I assume is a method) does two things at the same time; it breaks the String into pieces and displays those pieces. You should practice designing code so that methods like this does one thing only. Since you have two things, I would consider using two methods, one which breaks the String into pieces, and one which displays pieces of Strings.

5- If you decide to do #4, then you should investigate StringBuilder, like Campbell said, it would be smoother if your goal is to actually build a single String with line breaks (rather than break a String into pieces)

I hope you realize these are criticisms, they are meant as next-steps you can follow to improve both the code and you Java skills.
Carlos Valderrama
Greenhorn

Joined: Feb 20, 2013
Posts: 14
Thanks Steve i'll keep in mind all of your advices :-)

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading Text From a JtexArea
 
Similar Threads
Spliting a string on a second space and not all spaces
Read text file using delimter "\r\n" only
StringTokenizer class parsing \r\n string
Converting Text to Lines
I want to import single-spaced code