Results 1 to 5 of 5
  1. #1
    goodlily is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default Text format in JTextArea

    I have the following two strings and I would like to display them just as what I typed here in JTextArea.

    " a| b| c|"
    " aa| bb| ccc|"

    However, I noticed that JTextArea cannot line them up nicely becuase it treats the empty space different from regular characters. If I use Notepad or any other text editor, I can get nice format but JTextArea just cannot. I tried JTextPane, the same thing. Seems to me they give ' ' (space) half of the size of a regular character (like 'a').

    Any way to work around this? Thanks very much.

  2. #2
    goodlily is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Let me confirm:

    " a | b | c |"
    " aa| bb| ccc|"

    I gave space in the string so that the '|' can line up for these two strings. I can do this in Notepad, but not in JTextArea.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by goodlily View Post
    I have the following two strings and I would like to display them just as what I typed here in JTextArea.

    Java Code:
    "   a|    b|       c|"
    " aa|   bb|    ccc|"
    However, I noticed that JTextArea cannot line them up nicely becuase it treats the empty space different from regular characters. If I use Notepad or any other text editor, I can get nice format but JTextArea just cannot. I tried JTextPane, the same thing. Seems to me they give ' ' (space) half of the size of a regular character (like 'a').

    Any way to work around this? Thanks very much.
    The key to solving your problem is the font used for the JTextArea, and I'd try to use a monospaced font. e.g.:

    Java Code:
          int fontPoints = 12; // or whatever works best!
          myTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, fontPoints ));

  4. #4
    goodlily is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    it really worked. Appreciate !

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Different text color in JTextArea
    By bigvanilla in forum New To Java
    Replies: 2
    Last Post: 01-29-2011, 02:21 PM
  2. Format some text with Java
    By vampire in forum New To Java
    Replies: 0
    Last Post: 02-18-2010, 06:45 AM
  3. Need Help showing text in JTextArea
    By GuyFawkes in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2008, 09:19 AM
  4. JTextArea - text align
    By bradder in forum AWT / Swing
    Replies: 1
    Last Post: 11-29-2007, 07:08 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
  •