• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Making a JTextArea look good

 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaand again it's me.

I have succesfully set up my list of over 35-year-olds. What I did to make it look pretty is find the length of the longest name and add " " to the rest, so that it ends up looking like this:

name firstname
reallylongname firstname

It's just prettier. This is how I did it, in case it matters:



The problem is - I was going to append newLine to a JTextArea, so I can display it easily using Swing and print it as easily using text.print(). However, since " " is not as wide (pixelwise) as a letter, my wonderful order of lines (which works beautifully with sysout) is all jumbled. Is there a usefull way to align the items?

Thanks, M
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vietor wrote:Aaaaand again it's me.

Hello, you again.

I have succesfully set up my list of over 35-year-olds. What I did to make it look pretty is find the length of the longest name and add " " to the rest, so that it ends up looking like this:

....

The problem is - I was going to append newLine to a JTextArea, so I can display it easily using Swing and print it as easily using text.print(). However, since " " is not as wide (pixelwise) as a letter, my wonderful order of lines (which works beautifully with sysout) is all jumbled. Is there a usefull way to align the items?


If you absolutely need to use a JTextArea, you could set its font to be a mono-spaced font (i.e., Font.MONOSPACED) and append your data using one of the variations on the java.util.Formatter (String.format is one such variation). However one of the best ways I know to present table-like data is with a JTable in place of your JTextArea. Others may come up with even better suggestions -- the choice is yours.

Best of luck!
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:However one of the best ways I know to present table-like data is with a JTable in place of your JTextArea.

Indeed.
Formatting is much easier if one uses a JTable. You can even get rid of the grid lines by calling JTable#setShowGrid(false)
 
Martin Vietor
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Much better, I'm sure. I wasn't going to display the results on screen, just print'em directly. I figured JTextArea is easy to display while working on the project to see what it will look like. I am absolutely not set on it, so JTable it is.

Thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic