Specifying an alignment other than left for a specific style
Hi everybody,
I've been looking on the forum for this and haven't been able to find it (lots of similar stuff, which I've tried, but not quite this). I'm trying to make the first line in my JTextPane center aligned, and I tried to do it with this code:
Style def = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE );
Style header = doc.addStyle("header", def );
StyleConstants.setAlignment( header, StyleConstants.ALIGN_CENTER );
StyleConstants.setFontFamily( header,"Engravers MT" );
StyleConstants.setFontSize( header, 28 );
// and later on:
String headerLine = reader.readLine().concat("\n" );
doc.insertString( doc.getLength(), headerLine, header );
The font works fine, and so does the size; it's just the alignment that seems to be off. I only want that one style (and hence that one line) centered, not the whole JTextPane. Can anyone tell me what I'm doing wrong?
Thanks a million!
Jezzica85
[1119 byte] By [
jezzica85a] at [2007-11-27 8:40:44]

# 1
I'm no expert on this, but have you considered using JEditorPane and formatting the text with HTML?
# 2
Or you could follow this example of camickr's demonstrated here: http://forum.java.sun.com/thread.jspa?forumID=57&messageID=1408602&threadID=342068
# 3
I saw that example actually, and I thought that was for editing a whole pane, not one line. Could that be used for one line too, and I was mistaken? What exactly is a StyleEditorKit anyway? The javadocs don't explain that well.
EDIT: I just found out something else as well. The setForeground method to give the style a specific color also doesn't seem to be working.
Jezzica85
# 4
> I saw that example actually, and I thought that was for editing a whole pane,
Well look at the whole example. Some attributes are for the entire pane and some are for specific characters.
> Could that be used for one line too,
Sure, if you take the time to understand some of the concepts the demo is trying to show.
First you need to understand the difference between character and paragraph attributes. Character attributes are applied to individual characters. Paragraph attributes are applied to an entire line. Attributes are inherited from the attributes of the preceding character unless a new set of attributes is specified.
So in my simple example I use the center attribute on an empty document. Therefore any inserted text will inherit that attribute.
So you could try inserting the text using the character attributes for the entire text, like I do for the "keywords". Then you could try using the setParagraphAttribute() method. Now only the first line should be affected.
I'm no expert, thats why I create a SSCCE to play around with so I can do simple tests.
> What exactly is a StyleEditorKit anyway?
Its a class that provides the Actions that allow you to change the Font, Size, Style etc of the text.
# 5
Thanks camickr,
I guess I'll look at your example again and see if I can get it. It seems sort of weird to me though that things like StyleConstants.setFontFamily() and StyleConstants.setFontSize() work with no problem, and then we need to do this big workaround for alignment and color, even though there are StyleConstants.setForeground() and StyleConstants.setAlignment() methods. I wonder, is that a bug?
Jezzica85
# 6
> I wonder, is that a bug?
Just because you don't understand how something works doesn't make it a bug.
My simple example works fine. You didn't post a SSCCE so I can't comment on what you are doing right or wrong. In fact your posted code doesn't even show you playing with the foreground attribute.
I must admit I don't really understand using Styles either, which is why I keep it simple and use AttributeSets.
# 7
That wasn't what I meant--what I meant was, I thought that the setForeground and setAlignment methods would work as easily as the other ones. (just a simple little call to the style) I guess I was wrong, which as you said, doesn't necessarily make it a bug.
As for the foreground attribute, I forgot to put it in.
Anyway, like I said, I'll see what I can do and thanks for the tips.
Jezzica85
# 8
Well, I've got part of it working now--it was a silly mistake with text color enabling. I still can't seem to get the right alignment for the header row though. I've condensed my program down so it's self-contained, can anyone see where I'm making my mistake?
Thanks again!
Jezzica85
import java.awt.Color;
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 Test {
public static void main( String args[] ) {
try {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
JTextPane pane = new JTextPane();
StyledDocument doc = pane.getStyledDocument();
// Initialize text styles
MutableAttributeSet header = new SimpleAttributeSet();
StyleConstants.setAlignment( header, StyleConstants.ALIGN_CENTER );
StyleConstants.setFontFamily( header, "Engravers MT" );
StyleConstants.setForeground( header, Color.red );
StyleConstants.setFontSize( header, 28 );
MutableAttributeSet body = new SimpleAttributeSet();
StyleConstants.setAlignment( body, StyleConstants.ALIGN_LEFT );
StyleConstants.setFontFamily( body, "Monotype Corsiva" );
StyleConstants.setFontSize( body, 16 );
String headerLine = "header line";
doc.insertString( doc.getLength(), headerLine, null );
String text = "\ntext that should\nhave different formatting";
doc.insertString( doc.getLength(), text, null );
doc.setCharacterAttributes( 0, headerLine.length(), header, false );
doc.setCharacterAttributes( headerLine.length(), doc.getLength() - headerLine.length(), body, false );
frame.add( pane );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
} catch(Exception e){}
}
}
Message was edited by:
jezzica85
# 9
> Well, I've got part of it working now--it was a silly mistake with text color enabling.
Which is why I keep asking everybody to post a SSCCE. We waste time by looking at random pieces of code when we don't know what the rest of the program looks like.
> can anyone see where I'm making my mistake?
You missed my point about the difference between paragraph and character attributes. You can't set alignment with a character attribute. It doesn't make sense to have a single character in the line "center" aligned. You need to use the setParagraphAlignment(..) method, as my example does. Something like:
doc.setCharacterAttributes( 0, headerLine.length(), header, false );
// either of the following will work, hopefully you understand why
//doc.setParagraphAttributes( 0, headerLine.length(), header, false );
//doc.setParagraphAttributes( 0, 0, header, false );
I must admit I learned something by your example. Notice in my example I separated the paragraph and character attributes into two different AttributeSets, but it looks like you can mix character and paragraph attributes in the same AttributeSet. I guess somehow the set?. methods only use the appropriate attributes.
# 10
Ohhh, now I get it! That makes perfect sense now.Thanks for teaching me that!Jezzica85