• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StyledDocument and Paragraphs

 
Tim Holloway
Saloon Keeper
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone know how to programmatically add a paragraph to a Swing StyledDocument? All the examples I have construct a single-paragraph document and leave it at that, and the first/only paragraph is supplied by the document constructor behind the scenes.
 
Brian Cole
Author
Ranch Hand
Posts: 899
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Anyone know how to programmatically add a paragraph to a Swing StyledDocument? All the examples I have construct a single-paragraph document and leave it at that, and the first/only paragraph is supplied by the document constructor behind the scenes.

I'm not sure what you mean. Presumably this

int pos = yourStyledDocument.getLength();
yourStyledDocument.insertString(pos, "\nfoo", null);

does not meet your requirements?? Because (at least with DefaultStyledDocument) it does increase the paragraph count returned by

yourStyledDocument.getDefaultRootElement().getElementCount()

But then again doing something like this

yourJTextPane.setCaretPosition(pos);
yourJTextPane.replaceSelection("\nfoo");

will do the same thing to the underlying StyledDocument also, without messing with the Document directly.
Perhaps you could explain what you're trying to do.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic