1

I need to add items in a JtextArea and with its Description and a Price but i have a problem in aligning it. i want an output of this is like

Description          Price
Coffe                $50.00
Bread                $30.00
Egg                  $10.00

but my Program makes like

Description           Price
Coffe           $50
Bread                 $30
Egg                 $20

Anyone pls help me how to Align it?....

txtArea.append( "\n" + desc+"         " + price + "         " + quanty);
2
  • 2
    Yep, use a JTable. If you absolutely can't use a JTable, then make sure that you use a mono-spaced Font, such as Font.MONOSPACED, and then use String.format(...) not tabs. Commented Mar 19, 2015 at 14:50
  • I tried to use a jtable and it works fine.. but my problem is in JtextArea i cant align it... i used JtextArea instead of Jtable because i want to print the text on what is inside the JtextArea so it will work like A receipt. Commented Mar 19, 2015 at 14:54

1 Answer 1

3

HTML In a JTextPane rather than JTextArea one could use HTML:

<html><table><tr><th>Description</th><th>....<tr><td>...

With CSS styles the most beautiful, requiring some effort.

String.format With a fixed-size font (monospaced) in a JTextArea one can do:

txtArea.setFont(new Font("monospaced", Font.PLAIN, 12));
txtArea.append(String.format("%-30s %15s %10d\n", desc, price, quanty));
  • %-30s left aligned string
  • %15s right aligned string
  • %10d right aligned integer
6
  • It seems that your answer is Right sir but Im just a newbie in java and i havent tried to use jtextpane. and i did what you answered but it makes me mad on some errors that occur.. can you pls site another example sir? thank you.. Commented Mar 19, 2015 at 15:08
  • I was meaning, that HTML would be one solution, but with JTextArea there is a second solution, using String.format, Sorry for overcomplicating things.
    – Joop Eggen
    Commented Mar 19, 2015 at 15:11
  • its ok sir.. i tried it again and again... but i cant still get the right thing to align it, by the way sir i just want to use the JtextArea for printing a receipt.. but i think its not good to use JtextArea for printing a receipt. can u pls refer me a link or code how to make a receipt sir? thanks for taking time to answer my question. Commented Mar 19, 2015 at 15:21
  • I have added a setFont for a fixed-sized font, where every letter has the same width. Maybe that was the problem? For printing a receipt HTML is a better option (drawing lines, several fonts). In general swing components like JTextArea can be printed.
    – Joop Eggen
    Commented Mar 19, 2015 at 15:40
  • Finally...... Done. instead of txtArea.append(String.format("%-30s %15s %10d\n", desc, price, quanty));. i tried txtArea.append(String.format("\n" +"%-30s", desc)); txtArea.append(String.format(" %15s ", price)); txtArea.append(String.format(" %10d ", quanty)); thank you so much @Joop Eggen.. Commented Mar 19, 2015 at 15:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.