Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems w/ formatted text

 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to format some text which is being placed in a JTextArea in a JScrollpane. I am looping though an ArrayList and pulling out the values I need. I have set the String format as follows:

temp = String.format("%1$-20s %2$4s %3$6s%n", name, qty, price);

When the output appears in the JTextArea it looks kind of like the following:

However, if I copy the output from the JTextArea and paste it here, it looks great - exactly like I want it to.

Can anyone please tell me why it does not appear correctly in the JTextArea of my GUI?

Thanks,
Mike

[ EJFH: Added "CODE" tags to preserve formatting. ]
[ October 11, 2006: Message edited by: Ernest Friedman-Hill ]
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't get the question. These two outputs look exactly the same to me.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The font your JTextArea is using must be a [/i]proportional[/i] font, one in which all the characters aren't the same width. Most fonts are proportional fonts. If you set the font to be a fixed-pitch one ("Dialog", "Courier", etc) then things will meet your implicit expectation that all characters should be the same width.
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops - I should have looked at the post after I entered it. The second output, when pasted into this window (i.e. where you type your post), was lined up perfectly.

In any case, I'll try changing the font as suggested and see how it looks.

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Cleary:
The second output, when pasted into this window (i.e. where you type your post), was lined up perfectly.


That's because in most Web browsers, the textarea widget uses a fixed-pitched font!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic