Change font of an entire Document

I'm trying to create a simple text editor, with font, size and style controls. The component used is a JTextPane with a DefaultStyledDocument.

My problem is that I am finding it incredibly hard to change the font, size, and style of theentire document because there is an invisible line break at the end of the document that I can't delete and can't modify. Document.setCharacterAttributes doesn't work. I haven't had luck with the JTextPane's actions either. The only thing that has some effect is to call JTextPane.setFont() but this has problems too. as shown in bug #6525624.

This little line break causes many problems when I try to examine the Elements of the Document or export RTF.

Is there some way that I can get rid of this line break? I never wanted it in the first place! :) Thanks in advance.

[845 byte] By [RFM1980a] at [2007-11-26 20:08:35]
# 1
probarbly not the best way to do it, but how about yourTextpane.setText(""); just after you have constructed it?
alienchilda at 2007-7-9 23:11:21 > top of Java-index,Desktop,Core GUI APIs...
# 2

Don't know how your editor is working but here is one way to to change attributes for the entire document;

// Set alignment to be centered for all paragraphs

SimpleAttributeSet center = new SimpleAttributeSet();

StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);

StyleConstants.setSpaceBelow(center, 20.0f);

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

You can also set the character attributes (color, size, font..) the same way.

camickra at 2007-7-9 23:11:21 > top of Java-index,Desktop,Core GUI APIs...
# 3

That may change the appearance of all the text, but if you look at the structure of the Elements in your Document you will still find an extra line break at the end, and the attributes of that line break will not have been changed.

I still want to be able to either delete that linebreak or change its attributes.

RFM1980a at 2007-7-9 23:11:21 > top of Java-index,Desktop,Core GUI APIs...
# 4
Also, I should point out that the documentation for AbstractDocument states "AbstractDocument models an implied break at the end of the document." So maybe I can't delete that line break, but there must be some way to change its attributes.
RFM1980a at 2007-7-9 23:11:21 > top of Java-index,Desktop,Core GUI APIs...
# 5

> or change its attributes.

Don't understand the problem. It changes the attributes when I use it. That is if I type new text and the end of the document the attributes of the previous character are picked up.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-9 23:11:21 > top of Java-index,Desktop,Core GUI APIs...
# 6
You can use the above suggestion but add 1 to doc length to include the last element as well.doc.setParagraphAttributes(0, doc.getLength()+1, center, false);regards,Stas
StanislavLa at 2007-7-9 23:11:21 > top of Java-index,Desktop,Core GUI APIs...
# 7

OK, I get it now. Thanks very much everyone. You were right, doc.setCharacterAttributes(0, doc.getLength()+1, attrs, false) does work, but I was using it incorrectly. For what it's worth, I was doing this:

DefaultStyledDocument doc = new DefaultStyledDocument();

doc.setCharacterAttributes(0, doc.getLength()+1 , attrs, false);

doc.insertString(0,"Here is some sample text.", attrs);

JTextPane textPane = new JTextPane();

textPane.setDocument(doc);

when I should have been doing this:

DefaultStyledDocument doc = new DefaultStyledDocument();

doc.insertString(0,"Here is some sample text.", attrs);

doc.setCharacterAttributes(0, doc.getLength()+1 , attrs, false);

JTextPane textPane = new JTextPane();

textPane.setDocument(doc);

What I was doing before made the inserted text have the correct family, but the line break at the end was unchanged. My mistake. Thanks again, I will try to repay the forums by helping someone else out!

RFM1980a at 2007-7-9 23:11:21 > top of Java-index,Desktop,Core GUI APIs...
# 8
> but I was using it incorrectlyWhich was exactly the reason I asked for a SSCCE. Based on your verbal description we can't tell what your code is actually doing.And as I stated earlier, you don't need the "plus 1".
camickra at 2007-7-9 23:11:21 > top of Java-index,Desktop,Core GUI APIs...