Skip to main content

HOW do I set the horizontal alignment of a JXTextArea?

Please note these forums are being decommissioned and use the new and improved forums at
No replies
Joined: 2010-08-30

Hello SwingLabs :)

It's been few DAYS I'm STILL looking for the solution of a problem which first seemed to me TRIVIAL!!

Golden question: HOW do I set the horizontal alignment of a JXTextArea or JTextArea?

The API method setAlignmentY which is SUPPOSED to take care of this is doing NOTHING!!

Please don't tell me that this is a problem of JTextArea and hence of Swing not SwingX, because JXTextArea extends JTextArea and doesn't change anything with the alignment part, and hence it is as much GUILTY as JTextArea! :)

Strangest of all is that NONE of the forums addressing this same question answers DIRECTLY why setAlignmentY doesn't work as it should, or how it should be used.

All of them suggest workarounds like:

- Using JTextPane with attributes, i.e. something like:

StyledDocument doc = textPane.getStyledDocument();

SimpleAttributeSet center = new SimpleAttributeSet();

StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);

doc.setParagraphAttributes(0, doc.getLength(), center, false);

- Usign JTextField whose setHorizontalAlignment works!

I came to SwingLabs hoping to get a clear answer on this!

Please don't let me down. :)

Thanks a lot,