JTextPane with HTML problem

From:"Nicolas GASNIER" [ngasnier@masked-domain]
Sent on:Fri, 1 Sep 2006 09:29:23 +0200

I have a problem with JTextPane and HTML. I want to make a simple editor,
with the possibility to change some attributes like bold and color.

If I type "this word in bold", then select "word", press bold button, the
word is effectively displayed in bold in the editor, but it is not aligned
with the start of the line, and the "in bold" part is completely in the
right of the editor. I mean, the display is a little messed... Here is the
HTML code for that phrase :

<p style="margin-top: 0">

this <b>


</b>in bold


The string "word" has been replaced by a new paragraph tag, I don't
understand if this is because the editor is buggy or if i am doing something

Here is my creation code :

HTMLEditorKit kit = new HTMLEditorKit();
JTextPane messagePane = new JTextPane();

And the code I use to make the selected text in bold :

MutableAttributeSet style = new SimpleAttributeSet();
StyleConstants.setBold(style, true);
StyledDocument doc = messagePane.getStyledDocument();
int start = messagePane.getSelectionStart();
int end = messagePane.getSelectionEnd();
doc.setCharacterAttributes(start, end-start+1, style, true);

Note that I have tried to "register" my style with the text pane :

messagePane.addStyle("Bold", null);
StyleConstants.setBold(style, true);

Then, if I do the same thing, I get the following HTML code:

<p style="margin-top: 0">

this <b> <Bold> </b>in bold


Is there a workaround with this ? Or maybe I do not use the correct method
to achieve my goal ?
Please help me, I have searched for an answer all around the net, but not
found any...



Found what you were looking for? If not - continue at Wiki Index
Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register