aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Please help with JTextArea problem... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Please help with JTextArea problem..." Watch "Please help with JTextArea problem..." New topic
Author

Please help with JTextArea problem...

Jose Imenes
Greenhorn

Joined: Nov 06, 2006
Posts: 7
Hello-

I'm trying to output data from a file to a JTextArea and have encountered an unexpected problem...I want to display data in columns that not only line up with eachother but also line up with the column headers I've pre-inserted into the JTextArea when I build it (e.g. brand, model, price, color). How can I get columns of uniform width? The empty spaces between the text are a different width and are causing all kinds of alignment problems. I've tried to format the text first with String.format(%10s%10s%10s) etc, but this does not seem to work. Please help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You need to set the font used by the JTextArea to a "fixed pitch" font like Courier. Most fonts are "proportional" fonts, in which each character is a different width.

Or you could use a JTable.

I'm going to move this to our Swing/AWT/JFace/SWT forum.


[Jess in Action][AskingGoodQuestions]
Jose Imenes
Greenhorn

Joined: Nov 06, 2006
Posts: 7
Hello-

I'm using a monospace font, but still it doesn't work...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> but still it doesn't work...

post a sample program of how you are doing this

this is all that's required:
hard-code one line from the file:
String lineFromFile = "[whatever_it_is]"

a frame with a textArea in scrollpane,
add header line to textArea,
add lineFromFile to textArea.
Jose Imenes
Greenhorn

Joined: Nov 06, 2006
Posts: 7
while(inputFile.hasNext())
{
brand= inputFile.next();
model= inputFile.next();
color= inputFile.next();
quantity= inputFile.next();
line = String.format("%-7s%-20s%8s%12s\n", partNum, descrip,
quant, ppi);

//add fields to file
gui.getTextFields(line);
Jose Imenes
Greenhorn

Joined: Nov 06, 2006
Posts: 7
Hang on, I messed up copying the code, let me try that again.....

String header = String.format("%-7s%-20s%8s%12s\n\n"," Brand", "Model","Color", "Quantity");

while(source.hasNext())
{
brand = source.next();
model = source.next();
color = source.next();
quantity = source.next();
}
line = String.format("%-7s%-20s%8s%12s\n", brand, model, color, quantity);

textArea.append(header);
textArea.append(line);
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
If you are allowed to use a JTextPane instead of a JTextArea, then you can just use tab stops. It sounds simpler to me.

Here's an example.
[ November 20, 2006: Message edited by: Brian Cole ]

bitguru blog
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you want equal distance columns?

try this, but it is far, far easier using a JTable

Jose Imenes
Greenhorn

Joined: Nov 06, 2006
Posts: 7
Thanks very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help with JTextArea problem...