Article View: borland.public.jbuilder.java.swing
Article #186JTextPane with HTML problem
From: Nicolas GASNIER
Date: Fri, 01 Sep 2006 07:29
Date: Fri, 01 Sep 2006 07:29
62 lines
2794 bytes
2794 bytes
Hi,
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>
<p>
</b>in bold
</p>
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
wrong.
Here is my creation code :
HTMLEditorKit kit = new HTMLEditorKit();
JTextPane messagePane = new JTextPane();
messagePane.setContentType("text/html");
messagePane.setEditorKit(kit);
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
</p>
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...
Regards.
Nicolas.
Technical Details
Message-ID:
<44f7e14b$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 111d45,17959ffd31118524,start X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Reply-To: "Nicolas GASNIER" <ngasnier@rsid.fr> From: "Nicolas GASNIER" <ngasnier@rsid.fr> Newsgroups: borland.public.jbuilder.java.swing Subject: JTextPane with HTML problem Date: Fri, 1 Sep 2006 09:29:23 +0200 Lines: 64 Organization: RSID X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 NNTP-Posting-Host: 80.118.168.219 Message-ID: <44f7e14b$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1157095755 80.118.168.219 (1 Sep 2006 00:29:15 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail