Results 1 to 9 of 9
Thread: Carriage Returns in JTEXTAREA
- 04-17-2010, 12:36 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
Carriage Returns in JTEXTAREA
:confused:Hello. I posted this thread on the New To Java forum. However, no one answered! I suspect the problem is quite complicated. I was hoping someone here might be able to help me.
I have a JTEXTAREA to input some text. The input will feature carriage returns. Such as:
Line1 <carriage return>
Line2 <carriage return>....
However, I have some problems!
For example when I process the text i.e. count the number of lines the computer only sees it as one line.
When I save the text to a file (.txt format) a thin rectangle symbol is inserted for each carriage return. However, when I reopen the file using my program the computer recognises that I have multiple lines (through the use of the thin rectangle symbol!) The computer prints out the text showing that there are carriage returns and does not show the thin rectangle symbols!
One way around the problem could be to save the file first and then reload the file and process the text around the saved file! (However, I would rather the text file that was saved so that it does not have the thin rectangle symbols and instead shows a proper carriage return for each line.)
The code which receives the text from the user is stored in a string array as shown.
**** Start of code which sets up the input box and sends out the information ****
JTextArea area = new JTextArea();
area.setText(x);
area.setRows(20);
area.setColumns(30);
area.setEditable(true);
area.setLineWrap(true);
area.setWrapStyleWord(true);
JScrollPane scrollpane = new JScrollPane(area);
Object[] array = {new JLabel("Please edit or enter your text:"),
scrollpane, };
JOptionPane.showConfirmDialog(null, array, "Edit your text",
JOptionPane.OK_CANCEL_OPTION);
File file;
file=savefilepicker.filesaver();
savefilepicker.textwriter(area.getText() , file);
//The program passes the area.getText() to save the contents to the file.
*** END OF CODING FOR GETTING THE TEXT ***
I also list the code in savefilepicker.textwriter that saves the text to a text file. Remember it receives the text in the testarea by sending area.getText().
*** START OF CODE FOR SAVING TEXT TO FILE ***
FileWriter fstream = new FileWriter(filename);
BufferedWriter out = new BufferedWriter(fstream);
out.write(txt); /txt is a string containing area.getText()
//Close the output stream
out.close();
**** END OF CODE in savefilepicker.textwriter() ***
My questions are:
How does the content of area.getText() show that it has a carriage return in its format? Does the fact that I am using an object array somehow influence the formatting of the text in area.getText() ?
Also, is it possible to write the text to the text file so that it does not show a small rectangle, but instead a proper carriage return. I believe this would involve writing multiple lines.
I don't know if sending this code confuses the matter, but I would be very grateful for any help!
- 04-17-2010, 12:46 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
A JTextArea class extends from a JTextComponent class; the latter class defines a read( ... ) and write( ... ) method; those methods handle the burden of carriage return characters (\r) and line feed characters (\n). Internally a JTextArea (and a JTextComponent) uses a \n character as a line separator and it removes all \r characters.
kind regards,
Jos
- 04-17-2010, 12:57 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
Thank you for your post.
Does the last post mean that the getArea() will contain a \n for each new line? Therefore if I find a \n in the text this will represent a carriage return? (And no need to look for \r ?)
Many thanks
- 04-17-2010, 12:59 PM #4
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
Sorry I meant to write getText() not getArea().
Thank you for your post.
Does the last post mean that the getText() will contain a \n for each new line? Therefore if I find a \n in the text this will represent a carriage return? (And no need to look for \r ?)
Many thanks
- 04-17-2010, 01:07 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 04-17-2010, 02:12 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
What I want to do is take the text in getText() and count how many returns(to be precise the number of non printing characters other than spaces such as return) there are. I believe write and read are functions to do with files.
What do I have to look for in getText() to see if there is a carriage return? Do I count how many occurances of /n I can see?
- 04-17-2010, 02:49 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 04-17-2010, 06:21 PM #8
Member
- Join Date
- Apr 2010
- Posts
- 59
- Rep Power
- 0
Thank you I will give getLineCount() a go. Where can I find out about these methods?
I think the point you are missing is that I also wanted to save the text in getText() to file and have each line saved in a text file so it shows the carriage returns/new lines without the thin rectangles. How would you go about doing this?
Many thanks
Alec
- 04-17-2010, 07:34 PM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
The same place you find out about all Java methods. The Java API documentation.Where can I find out about these methods?
And the answer was given in the first reply. So read the API for the method that was given to you.I think the point you are missing is that I also wanted to save the text in getText() to file
You can't program without reading the API.
Similar Threads
-
String file and carriage returns
By AJArmstron@aol.com in forum New To JavaReplies: 2Last Post: 04-17-2010, 01:28 AM -
Waiting for a carriage return
By Ebodee in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:46 AM -
JTextArea on PopUp -JTextArea isn't editable
By Richy76 in forum AWT / SwingReplies: 3Last Post: 02-01-2010, 07:51 PM -
Checking if string contains only tabs/spaces/newlines/carriage returns
By Singing Boyo in forum New To JavaReplies: 3Last Post: 08-20-2009, 03:57 PM -
New line or Carriage Return through FileWriter
By johnt in forum New To JavaReplies: 2Last Post: 05-20-2007, 09:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks