Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

alignment of text in JTextArea  RSS feed

 
D Preethi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a requirement in which I want to center align the text in a JTextArea. I tried google for doing this, and most of the forums seem to suggest that I should use JTextPane instead of JTextArea. I have a constraint due to which I cannot use JTextPane. Is there no way to achieve center alignment of text in JTextArea?

Thanks,
Preethi
 
Stephan van Hulst
Saloon Keeper
Posts: 6980
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not exactly sure if this will work, it's a wild guess:

Create a DefaultStyledDocument, and maintain a reference to it so you can change attributes like text alignment. Then when you create your JTextArea, pass this document to its constructor.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe JTextArea doesn't use the style information of StyledDocument.
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and most of the forums seem to suggest that I should use JTextPane instead of JTextArea


I would guess all the suggestions would be to use a JTextPane.

I have a constraint due to which I cannot use JTextPane.


Why?

Is there no way to achieve center alignment of text in JTextArea?


You could copy all the code from the JTextPane classes and views and add it to a JTextPane, but then I guess you would just have another JTextPane, but with a lot more effort.

You could create a custom component that use a TextLayout and LineBreakMeasure and then paint all the text yourself.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!