aspose file tools*
The moose likes Beginning Java and the fly likes Problems w/ formatted text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems w/ formatted text" Watch "Problems w/ formatted text" New topic
Author

Problems w/ formatted text

Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
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 ]

Mike<br />SCJP 1.4<br />----------------------------<br />mdcleary@earthlink.net<br />----------------------------<br />There are 10 types of people<br />in the world. Those that <br />understand binary, and those<br />that don't.
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
I don't get the question. These two outputs look exactly the same to me.


Darryl Failla
Sun Certified Java 2 Programmer
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!
 
Don't get me started about those stupid light bulbs.
 
subject: Problems w/ formatted text