Skip to main content

How to center each text row in a JTextArea?

7 replies [Last post]
jacek
Offline
Joined: 2003-08-22

There is no setHorizontalAlignment() function in a JTextArea. How do I center each row of text?
(I am using setLineWrap(true) to wrap text to multiple rows)

There seems to be no easy way of doing it...

thx,
Jacek

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
zander
Offline
Joined: 2003-06-13

Look at the document, not the widget.

jacek
Offline
Joined: 2003-08-22

Could you elaborate in more detail? I looked at the Document object, but can't really see anything pertaining to centering the horizontal alighment of each line...

zander
Offline
Joined: 2003-06-13

To be precise the javax.swing.text.html.HTMLDocument should have more info, although I'm pretty sure you should manage with its superclass; DefaultStyledDocument

See;
http://java.sun.com/docs/books/tutorial/uiswing/components/text.html
and
http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html

jacek
Offline
Joined: 2003-08-22

I'm having a lot of issues trying to get this to work. The text displays, but is still not centered even though I explicitly set the style attributes. Is there something I am
doing wrong here?

[code]
SimpleAttributeSet attrib = new SimpleAttributeSet();
StyleConstants.setAlignment(attrib, StyleConstants.ALIGN_CENTER);
DefaultStyledDocument lsd = new DefaultStyledDocument ();
try {
lsd.insertString(0,text,attrib);
} catch (Exception ex) {}

label = new JTextArea(lsd);
label.setEditable(false);
label.setLineWrap(true);
label.setWrapStyleWord(true);
[/code]

It wraps to multiple lines, but still left justified, instead of centered...

zander
Offline
Joined: 2003-06-13

> something I am doing wrong here?
No idea; I just found that the Stylepad demo (comes with your jsdk) has centered text; take a look at that.

scott.violet
Offline
Joined: 2006-02-17

You had originally asked about JTextArea, are you ok with using
JTextPane?
Unless you create your own ViewFactory and potentially custom Views
JTextArea isn't going to handle centered text. Thankfully JTextPane
can.

Here's an example of making all paragraphs centered with a JTextPane:

[code]
StyledDocument document = new DefaultStyledDocument();
Style defaultStyle = document.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(defaultStyle, StyleConstants.ALIGN_CENTER);
JTextPane textPane = new JTextPane(document);
[/code]

-Scott

jacek
Offline
Joined: 2003-08-22

Yes, but there is a major problem with JTextPane...it doesn't support setWrapStyleWord(true) for line wrapping. I want it to wrap at the word boundary, not randomly in the middle of each word, which looks very ugly.

Is there any way to force JTextPane to wrap each line by word, instead of by letter?

[EDIT]...Oops, seems I spoke too early...word wrapping seems to work just fine. Thanks for pointing me in the right direction!

Message was edited by: jacek