JEditorPane vs JTextPane vs JTextArea for center align text and word-wrap

45 Views Asked by At

I'm trying to display some center align text that also word wraps.

My initial thought was using a JTextArea but I found no way to center align the text.

For this I found most people recommended using a JTextPane, which it allowed me to align the text as I wanted but it does not wrap the text, it just keeps all the text in one line.

This how I found a way to center align the text with the JTextPane

        textPane = new JTextPane();
        textPane.setPreferredSize(new Dimension(240, 60));
        StyledDocument doc = textPane.getStyledDocument();
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        doc.setParagraphAttributes(0, doc.getLength(), center, false);

I didn't find a way to set the wrap with the JTextPane, what I did find was most people recommend using a JTextArea or a JEditorPane.

The JEditorPane does a single-letter wrap but once again I didn't find a way to center align the text.

So, is there a way to have both a text wrap and center align text? Which component would be better for it?

0

There are 0 best solutions below