Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JTextArea windows friendly line break

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    715
    Thanks
    0
    Thanked 131 Times in 114 Posts

    Default JTextArea windows friendly line break

    Hi there.

    I have a JTextArea in a little GUI application. The user has the possibility to save the text from this area to disk in a simple txt file.
    Problem is, the line break character used by the JTextArea is not a windows friendly line break.
    The text document shows the line breaks correctly when opening the files with "good" editing software like Notepad++, etc, but the good old standard ms editor does not display line breaks at all.

    My question is, can I somehow change this behavior of the JTextArea? I would like to use "\r\n" which should work with all machines instead of just using "\r" or "\n".
    I know that I could manually go over the text and change all occurances of the character "\r" to "\r\n" and "\n" to "\r\n", but I would rather not do that if such functionality already exists.

    Thank you very much for the answers.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: JTextArea windows friendly line break

    You should be able to initialize a line feed/carriage return character for the host OS using

    System.getProperty( "line.separator" );

    Let us know how it works out.

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    715
    Thanks
    0
    Thanked 131 Times in 114 Posts

    Default Re: JTextArea windows friendly line break

    But how would that help me to change the way the JTextArea works? Or am I missing something?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: JTextArea windows friendly line break

    I haven't completely thought it through, but using that (maybe), with this link "Newlines" on the JTextArea API, and the discussion at the destination, Class DefaultEditorKit, something should be possible to either influence how the JTextArea operates, or how the host OS responds to how it operates. There also might be something to be done with the underlying Document Model.

    Some pieces to consider, but not a final answer.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    715
    Thanks
    0
    Thanked 131 Times in 114 Posts

    Default Re: JTextArea windows friendly line break

    I see.
    That link was very helpful, thank you very much.

    The article says that:
    When a document is loaded, EndOfLineStringProperty is set appropriately, and when the document is written back out, the EndOfLineStringProperty is used. But while the document is in memory, the "\n" character is used to define a newline, regardless of how the newline is defined when the document is on disk.
    So there is no way to change the behavior of the getText() method. I either use the document that is backing the JTextArea or I replace those strings myself.

Similar Threads

  1. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  2. employing library jar from windows command line
    By meathead in forum Other Programming Languages
    Replies: 6
    Last Post: December 12th, 2011, 03:49 PM
  3. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  4. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Code Snippets and Tutorials
    Replies: 10
    Last Post: March 31st, 2011, 05:18 AM
  5. Super Friendly & Nice Java Forum!
    By friendly_jessie21 in forum Member Introductions
    Replies: 2
    Last Post: February 5th, 2011, 02:28 PM