Search code examples
javaswingtextjtextpanetabular

Align JTextPane and measure text length


I'm trying to make JTextPane with two columns, both aligning to left like that:

Column A:    Column B:
Alpha        Alpha
Beta         Beta
Gama         Gamma

Is there anyway to do this on JTextPane, and if not, what can I use for it?


Solution

  • You can simply use tabulation to create columns "visibility":

        JTextPane pane = new JTextPane ();
        pane.setText ( "Column A:\tColumn B:\n" +
                "Alpha\tAlpha\n" +
                "Beta\tBeta\n" +
                "Gama\tGamma" );
    

    In some cases one "\t" might not be enough to create equal spacing (for example if you will have some long text in 1st column).

    Anyway, its better choice to use JTable instead, if you need a table-like structure.