Forums Register Login

Making a JTextArea look good

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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!
+Pie Number of slices to send: Send
 

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)
+Pie Number of slices to send: Send
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!!
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 2031 times.
Similar Threads
Dual Program (Applet & Application) : Need to know how to improve in Applet ways only!
MultiDimenaional Array sort
Making Typing "Game", any tips or pointers before I start?
Parsing out names from String
Create a Name class to manage different name changes of a person.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 03:51:54.