Aligning Text In JtextArea in java

后端 未结 1 1255
谎友^
谎友^ 2021-01-24 06:42

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                


        
相关标签:
1条回答
  • 2021-01-24 07:06

    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
    0 讨论(0)
提交回复
热议问题