Results 1 to 4 of 4
  1. #1
    gabel8 is offline Member
    Join Date
    Jan 2014
    Posts
    27
    Rep Power
    0

    Default JTextArea and new line issue

    I have an application that creates a string that is meant to write several lines, each on their own line. For example, a string is created as:

    Java Code:
    String multLineString = "This is line 1\nThis is line 2\nThis is line 3.";
    This string is then stored in the database in a text field. I have an option in the application that is able to retrieve the variable from the database and display it in a JTextArea field. However, when I retrieve it and display it, it is not displaying as multiple lines but it is displaying the string as 1 line (minus the \n). I tried to just retrieve the string variable and do a System.out.println and it does indeed print it on multiple lines. I can't figure out why the JTextArea displays it differently. Any suggestions?

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: JTextArea and new line issue

    why the JTextArea displays it differently.
    Can you post a small, simple program that compiles, executes and shows the problem.
    If the System.out.println does print it on multiple lines, what happens to the \n in the String between that statement and when it is added to the text area?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: JTextArea and new line issue

    Reading the API for JTextArea there is this section:
    Quote Originally Posted by api
    Newlines
    For a discussion on how newlines are handled, see DefaultEditorKit.
    And DefaultEditorKit explains how that works.

    Put simply, it's possible the new line character(s) isn't set up correctly.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    gabel8 is offline Member
    Join Date
    Jan 2014
    Posts
    27
    Rep Power
    0

    Default Re: JTextArea and new line issue

    I have multiple classes that accomplish this but in the class that displays and sets it, here are the couple of lines:

    Setting the variable:
    Java Code:
    myObject.setLineInfo("Line 1\r\nLine 2\r\nLine 3");
    Displaying the variable:
    Java Code:
    txtArea.setText(myObject.getLineInfo());

    Like I said before, if I view the value in the database, the lines are on different lines and if I do a System.out.println on the line right before setting the text area, it prints them on different lines.

Similar Threads

  1. how do i get the next line for a jtextarea?
    By javaamateur1 in forum New To Java
    Replies: 1
    Last Post: 08-20-2011, 06:03 PM
  2. Highlighting line in JTextArea
    By misiom1 in forum New To Java
    Replies: 4
    Last Post: 01-15-2011, 01:39 PM
  3. JTextArea How To get one Line only..?
    By Dinesh_rockz in forum AWT / Swing
    Replies: 2
    Last Post: 01-12-2011, 05:25 PM
  4. JtextArea autoamtic update the last line
    By sircurse in forum NetBeans
    Replies: 1
    Last Post: 10-28-2010, 07:08 PM
  5. How to always show the last line in my JTextArea?
    By Ashley in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:01 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
  •