• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help with JTextArea problem...

 
Jose Imenes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jose Imenes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello-

I'm using a monospace font, but still it doesn't work...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 899
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you want equal distance columns?

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

 
Jose Imenes
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic