Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
853 views
in Technique[技术] by (71.8m points)

swing - Java - Align JTextArea to the Right

I it possible to align the text inside a JTextArea to the right (or change the text alignment in general)?

|Left         |
|  Centered   |
|        Right|    <- Like this

I've been searching for hours and it seems others have asked this question before but there are no good answers (that actually work).

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try with JEditorPane or JTextPane instead of JTextArea.

Please have a look at my another post JEditorPane vertical aligment for complete sample code.

For more info have a look at this thread Vertical Alignment of Text in JEditorPane

Sample code:

JTextPane output = new JTextPane();

SimpleAttributeSet attribs = new SimpleAttributeSet();
StyleConstants.setAlignment(attribs, StyleConstants.ALIGN_RIGHT);
output.setParagraphAttributes(attribs, true);

EDIT

You can try

JTextArea jTextArea = new JTextArea();
jTextArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

Read more about How to set the orientation of JTextArea from right to left


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...