0

I want to display text in multimple rows and text align right.

I tryed to put it in JTextArea with ComponentOrientation.RIGHT_TO_LEFT but punctuation (?!) is not correct displayed.

enter image description here

Community
  • 1
  • 1

2 Answers2

2

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);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • The problem is the text don't have auto wrap [img](http://puu.sh/lMOiQ/f50b3ba62c.png) – Adrian Simionescu Dec 07 '15 at 17:00
  • @AdrianSimionescu, A JTextPane does line wrapping by default. You are doing something in your code to turn this feature off. Post a proper [SSCCE](http://sscce.org/) to demonstrate the problem, not a picture. See [No Wrap Text Pane](https://tips4java.wordpress.com/2009/01/25/no-wrap-text-pane/) for more information. – camickr Dec 07 '15 at 18:08
1

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>");
Andre
  • 778
  • 1
  • 5
  • 23
  • But It is a chat post. I do not know where is
    therefore I need auto wrap. Text input is a paragraph that must be displayed on several occasions .
    – Adrian Simionescu Dec 07 '15 at 17:15