Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default 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!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default

    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

  3. #3
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    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

  4. #4
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    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

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by AJArmstron@aol.com View Post
    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
    The JTextArea uses \n characters (line feeds) for line separators, not carriage return characters \r. If you let those read( ... ) and write( ... ) methods do their job you have nothing to worry about (i.e. they handle the conversion).

    kind regards,

    Jos

  6. #6
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Question

    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?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,995
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by AJArmstron@aol.com View Post
    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?
    You need glasses ;-) there's a method getLineCount() ...

    kind regards,

    Jos

  8. #8
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Cool

    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

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Where can I find out about these methods?
    The same place you find out about all Java methods. The Java API documentation.

    I think the point you are missing is that I also wanted to save the text in getText() to file
    And the answer was given in the first reply. So read the API for the method that was given to you.

    You can't program without reading the API.

Similar Threads

  1. String file and carriage returns
    By AJArmstron@aol.com in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 01:28 AM
  2. Waiting for a carriage return
    By Ebodee in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 03:46 AM
  3. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  4. Replies: 3
    Last Post: 08-20-2009, 03:57 PM
  5. New line or Carriage Return through FileWriter
    By johnt in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •