0

How can I align text using Javax.swing. as the numbers are aligned in the pic:

enter image description here

TimeToCode
  • 901
  • 2
  • 16
  • 34
Excali
  • 9
  • 1
  • 4

1 Answers1

0

You can achieve it with using HTML and JLabel. It allows you to insert tabulators in your text. To make them visible you have to use <pre></pre> tags. Consider this code, hope it helps.

public class Main extends JFrame {

    JLabel text;

    public Main() {
        String html = "<html><pre>xxxx\t\txxxx<br>xxxxxxxxxxxxxx\txxxx</pre></html>";
        text = new JLabel(html);
        text.setPreferredSize(new Dimension(200,200));

        add(text);
        pack();
        setVisible(true);
    }

    public static void main (String [] args) {
        new Main();
    }
}
Wojtek
  • 1,288
  • 11
  • 16
  • 1
    *"It allows you to insert tabulators in your text."* Better if using HTML would be a `table` structure. Tabs could be used in a `JTextArea` using a monospaced font. – Andrew Thompson Sep 25 '16 at 01:45
  • Good notice but I think tabs should work not only with monospaced fonts. – Wojtek Sep 25 '16 at 07:43