Align Strings in columns in JTextArea

后端 未结 4 1208
渐次进展
渐次进展 2020-12-10 06:54

I want to print Strings in JTextArea and align them properly. Its hard to explain so I will upload the screen shot of what I am trying to achieve.

相关标签:
4条回答
  • 2020-12-10 07:04

    Output will be aligned "properly" in your JTextArea only if you use a mono-spaced font. "Andale Mono 14" for example would do the trick.

    Also, in order to make your life easier and avoid the padding hell, use String.format with it's syntax.

    String format = "%1$5s %2$-40s %3$-20s";
    String someLine;
    while (whatEver...) {
       ... 
       someLine = String.format(format, aNum, aName, aDate);
       jTextArea1.append(someLine + "\n");
    }
    
    0 讨论(0)
  • 2020-12-10 07:14

    Use a JTable instead (for what is apparently tabular information). See How To Use Tables for more details & working examples.

    Table Sort Demo

    0 讨论(0)
  • 2020-12-10 07:15

    You can also change the font of the JTextArea if it is allowed in your problem

    textArea.setFont(new Font("monospaced", Font.PLAIN, 12));
    
    0 讨论(0)
  • 2020-12-10 07:31

    You may use HTML with swing component or use JEditorPane.

    JLabel jt=new JLabel();
    jt.setText("<html>
                <table border='1'>
                   <tr><th>No</th><th>Name</th></tr>
                   <tr><td>1</td><td>Mr.A</td></tr></table></html>");
    
    0 讨论(0)
提交回复
热议问题