• Post Reply Bookmark Topic Watch Topic
  • New Topic

Format String - 2 columns - JText Area  RSS feed

 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see from other post that I need to format a string, but not clear on how to do this. As you can see below I am trying to make two columns (Item and Item's cost). Because the length of the item varies,
this throws of my \t for spacing purpose. I know why it is not working, but not clear on how to fix the two columns, so the they line up. From what I read, I use a string format and set the number of characters after a % sign.

Any clarification is greatly appreciated. Thanks, Brent



 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to catenate the three tabs like that. "\t\t\t" will work.
A tab character takes you to a certain distance from the start of the line, so "123\t" will probably take you somewhere to the left of "Brent Van Scoy\t"
There are several ways to get nice columns.
One is to use the % tags and give a “width” to each column. "%-24s%6.2f%n" will give you 24 characters left‑justified then a number like 123.45 occupying up to 6 spaces. go through the Java Tutorials with ctrl‑F‑formatting. There are at least two places where you will find about format strings.
A second is to use two text areas next to each other.
Another is to use a table.
There are bound to be other solutions, too.
 
Brent Van Scoy
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have read the Java Tutorials, but I am uncertain how to use .format in the Jtext area to create the two columns.

<<One is to use the % tags and give a “width” to each column. "%-24s%6.2f%n" >>

This is what I was attempting to do, but I don't see an example on how to do this with the JText Area as part of my loop.

Maybe it is before this line? or Maybe it is inline? That is what I am not clear about.



So far in class we have only formatted the JText area with \n and \t

I will keep reading. Thanks
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not a Swing expert - I'm barely a novice. But remember you can separate out two pieces here.

There is the CONSTRUCTION of the string you need, and there is the DISPLAY/ADDING IT TO THE JText.

your JText area just wants a String. It doesn't care how you create the string. so, can you create teh string, formatted how you want it, somewhere else, and then add/pass THAT String to your widget?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!