I want to display text in multimple rows and text align right.
I tryed to put it in JTextArea
with ComponentOrientation.RIGHT_TO_LEF
T but punctuation (?!) is not correct displayed.
I want to display text in multimple rows and text align right.
I tryed to put it in JTextArea
with ComponentOrientation.RIGHT_TO_LEF
T but punctuation (?!) is not correct displayed.
You can't use a JTextArea
for this.
Instead you need to use a JTextPane
with custom paragraph attributes.
Here is an example that centers the text on each line.
JTextPane textPane = new JTextPane();
textPane.setText( "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight" );
StyledDocument doc = textPane.getStyledDocument();
// Set alignment to be centered for all paragraphs
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
Why not just use html in the JTextArea, Java will display it correctly. Example:
JLabel example = new JLabel();
example.setText("<html>This is the first line<br>This is the second</html>");