Centering a single line in the middle of a JTextPane

Hi everybody,

I'm trying to center a line of text designated as a divider on a JTextPane, and only that text. For some reason, though, when I do that, it centers all the text below the divider on the JTextPane too. The relevant code for this is:

MutableAttributeSet bodyCenter =new SimpleAttributeSet();

StyleConstants.setAlignment( bodyCenter, StyleConstants.ALIGN_CENTER );

StyleConstants.setFontFamily( bodyCenter,"Book Antiqua" );

StyleConstants.setFontSize( bodyCenter, 18 );

MutableAttributeSet body =new SimpleAttributeSet();

StyleConstants.setAlignment( body, StyleConstants.ALIGN_LEFT );

StyleConstants.setFontFamily( body,"Book Antiqua" );

StyleConstants.setFontSize( body, 18 );

String divider =" ";

doc.insertString( doc.getLength(), divider, bodyCenter );

int length = doc.getLength() - divider.length();

doc.setParagraphAttributes( length, length + divider.length(), bodyCenter,false );

doc.insertString( doc.getLength(),"\n", body );

I'm doing the same thing farther up and it works, so I think there must be some weird cut and paste error I'm missing or something. I can put up a self-contained program if nothing jumps out, but I don't think we'll need one. Can anyone please help me out?

Thanks,

Jezzica85

[1576 byte] By [jezzica85a] at [2007-11-27 11:51:57]
# 1

> Can anyone please help me out?

Not this time.

I'm tired of asking for a SSCCE every time to see the current behaviour in an attempt to understand what you are asking for.

Did your last posting of the JFrame Border not convince you that if we can play with a simple demo then we can understand and explain the problem better?

camickra at 2007-7-29 18:41:25 > top of Java-index,Desktop,Core GUI APIs...
# 2

A simple "you need a program this time" would have been more than sufficient. I don't always realize when I do or don't need to post a program, because there have been times were I haven't needed to. I did say that if I needed one--which it looks like I do--that I would post one. And yes, the last posting did teach me the value of demos, which was why I offered to post one.

I'm going to make up a demo and post it, but it'll take me a minute. You don't have to reply again if you don't want to.

jezzica85a at 2007-7-29 18:41:25 > top of Java-index,Desktop,Core GUI APIs...
# 3

Here's my self-contained program if anyone still wants to give me a hand; thank you.

import javax.swing.JFrame;

import javax.swing.JTextPane;

import javax.swing.text.MutableAttributeSet;

import javax.swing.text.SimpleAttributeSet;

import javax.swing.text.StyleConstants;

import javax.swing.text.StyledDocument;

public class WindowTest {

public static void main(String args[]) {

try {

JFrame frame = new JFrame();

frame.setSize( 400, 400 );

JTextPane pane = new JTextPane();

StyledDocument doc = pane.getStyledDocument();

MutableAttributeSet body = new SimpleAttributeSet();

StyleConstants.setAlignment( body, StyleConstants.ALIGN_LEFT );

StyleConstants.setFontFamily( body, "Book Antiqua" );

StyleConstants.setFontSize( body, 18 );

MutableAttributeSet bodyCenter = new SimpleAttributeSet();

StyleConstants.setAlignment( bodyCenter, StyleConstants.ALIGN_CENTER );

StyleConstants.setFontFamily( bodyCenter, "Book Antiqua" );

StyleConstants.setFontSize( bodyCenter, 18 );

doc.insertString( doc.getLength(), "A test string\n\n\n", body );

String divider = "divider";

doc.insertString( doc.getLength(), divider, bodyCenter );

int length = doc.getLength() - divider.length();

doc.setParagraphAttributes( length, length + divider.length(), bodyCenter, false );

doc.insertString( doc.getLength(), "\n", body );

doc.insertString( doc.getLength(), "Another string\n", body );

doc.insertString( doc.getLength(), "Yet another string", body );

frame.add(pane);

frame.setVisible( true );

} catch( Exception e ) {

e.printStackTrace();

}

}

}

jezzica85a at 2007-7-29 18:41:25 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I don't always realize when I do or don't need to post a program,

That has been my point for the last number of postings. You don't know when you need to post one, so always post one.

If you've written code that doesn't do what you want it to do then post the code so we can see what its doing and you can explain whats wrong.

Obviously there are times when you can ask a question and people will recognize the sympton and be able to provide an answer, buy why wait for a couple of hours hoping someone answer the question and then post the code only when asked. After all it only took you 10 minutes to provide the code once asked so its really not that much trouble now is it?

camickra at 2007-7-29 18:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 5

Point taken. I guess I just have to get into the habit.

jezzica85a at 2007-7-29 18:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 6

[caveat: I have no idea what I'm doing; I'm just throwing code on the screen]

But what if you add this statement?:

doc.insertString(doc.getLength(), "\n", body);

doc.insertString(doc.getLength(), "Another string\n", body);

doc.insertString(doc.getLength(), "Yet another string", body);

doc.setParagraphAttributes(length + divider.length() + 1, doc.getLength(), body, false); // !! this line !!

petes1234a at 2007-7-29 18:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 7

Hi Pete,

You must have some idea of what you're doing; that worked!

Thanks a lot!

Jezzica85

jezzica85a at 2007-7-29 18:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 8

When inserting text the attributes are inherited from the previous text unless the attributes are changed. As I tried to explain in your previous posting on this topic paragraph attributes are not set when you insert text using an AttributeSet, so you need to set them explicitly with the setParagraphAttributes(...) method. And since the entire line has the same attribute you only need to specify a single offset of the line in question to change the paragraph attributes.

You may find the following easier to implement.

doc.insertString( doc.getLength(), "A test string\n\n\n", body );

doc.insertString( doc.getLength(), "divider", bodyCenter );

doc.setParagraphAttributes( doc.getLength(), doc.getLength(), bodyCenter, false );

doc.insertString( doc.getLength(), "\n", body );

doc.setParagraphAttributes( doc.getLength(), doc.getLength(), body, false );

doc.insertString( doc.getLength(), "Another string\n", body );

doc.insertString( doc.getLength(), "Yet another string", body );

camickra at 2007-7-29 18:41:26 > top of Java-index,Desktop,Core GUI APIs...