This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Making a JTextArea look good Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Making a JTextArea look good" Watch "Making a JTextArea look good" New topic
Author

Making a JTextArea look good

Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
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


The problem people have in trying to create something foolproof is that they generally underestimate the inginuity of fools.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Jul 26, 2007
Posts: 10172
    
    8

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)


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Martin Vietor
Ranch Hand

Joined: Oct 31, 2008
Posts: 40
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!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Making a JTextArea look good
 
Similar Threads
Create a Name class to manage different name changes of a person.
Making Typing "Game", any tips or pointers before I start?
MultiDimenaional Array sort
Dual Program (Applet & Application) : Need to know how to improve in Applet ways only!
Parsing out names from String