File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes How to insert HTML content directly in code from JEditorPane ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to insert HTML content directly in code from JEditorPane ?" Watch "How to insert HTML content directly in code from JEditorPane ?" New topic

How to insert HTML content directly in code from JEditorPane ?

med lam

Joined: Jul 17, 2001
Posts: 3

I'm developping an HTML Editor on a JEditor Pane.
The problem is that a blank is not interpreted, and i've tried to add "& n b s p ;" in the code from a KeyListener added to the VK_SPACE event, or to define an Action to add HTML Spaces. But i didn't achieve to...
I just want to insert a "& n b s p ;" in the HTML code of the document displayed in the JEditor evry time the SpaceBar is pressed.
I've tried InsertContentAction() :
Not a solution :It inserts " & n b s p ;" in the text (it appears litteraly).
I've tried InsertHTML. But it needs a tag parameter, and in my case, i need no tag
to be inserted.
I'm stuck since days... Any help woul be extremely appreciated...

Ranch Hand

Joined: Jun 21, 2001
Posts: 57
After creating a JEditorPane have u called setContentType("text/html"). If so then JEditorPane will look for an EditorKit registered as supporting that type, and will find the HTMLEditorKit by default.
More over your "& n b s p". should not have any space in between(" ").

med lam

Joined: Jul 17, 2001
Posts: 3
Sorry to disappoint you but i've already did all that stuff.
I've even created my own EditorKit, HTMLDocument, And extended the parser... The problem isn't that simple.
Moreover, you've noticed that when you don't put spaces between & n b s p it does'nt appear on the forum message (as in yours) . My question was actually how to insert content (as spaces) and not how to set a JEditorPane to read and write HTML, caus' this, i know already...

AjayKumar Kanukuntla

Joined: Oct 18, 2001
Posts: 6
i was building help screens for my project using HTML.
But due to the default parser 3.2 DTD, i couldnot correctly display the frames and other html controls.
i saw in your posting that u could use 4.0 DTD for your purpose.
Has it solved the problems mentioned above?
Please help me in extending to 4.0 DTD.
Eric Low
Ranch Hand

Joined: Mar 14, 2002
Posts: 41
Hi Med Lam
Actually I have faced EXACTLY the same problem currently, ie trying to insert some special character as it is into JTextPane. I wonder if you have found any solution/workaround ? Need help urgently!
Thanks in advance!
Eric Low
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
I just found the class HTMLDocument.HTMLReader. you might handle space characters in a custom subclass of it?!
Eric Low
Ranch Hand

Joined: Mar 14, 2002
Posts: 41
I don't much about the HTMLDocument.HTMLReader for the time being. However, I have learned to use an Unicode equivalent as a workaround:

The above will insert a space equivalent ie "&#160;" into the HTML. One more thing, I am still looking for a Unicode equivalent for <br>. Any idea?
Jaunty John

Joined: Jul 14, 2002
Posts: 21
In HTML, "&nbsp;" (without the quotes) is a code character sequence to the server/browser to include an additional space in the DISPLAY of the document. It is an HTML standard to only recognise a single space (the ASCII character 32) for display purposes, and ignore multiple spaces in the stream of HTML instructions. The "&nbsp;" code is to tell the browser that more than one space is wanted in the display of the document. So, in an HTML document, creating an "empty" cell in a table looks like this: <TD>&nbsp;</TD>. And, if your desire is to have four spaces displayed, you will need to write something like "&nbsp;&nbsp;&nbsp;&nbsp;" (again, no quotes) to display four spaces, which would appear as "    " (quotes used to display 4 spaces).
If you want to insert the character sequence, "&nbsp;" into an HTML code page, (to indicate to the browser to display a space) then code your java editor to decode something like hitting F12 to mean: "insert ´&nbsp;´ at the cursor" (again without the quotes). Remember, computers do exactly what you tell them to do. If you hit the space bar while in a text editor, it inserts a space in the text.

(((o o)))<br />""--^--"" Entropy is Increasing...
k kr

Joined: Jul 08, 2011
Posts: 1
i had resolved this problem, thanks to all of your previous posts.

i had an WYSIWYG editor with source code preview. after switching to code and back, all multiple spaces where always gone (because of html works).

i just overriden the space keystroke action to insert the \u00A0 sign instead of space. all is working correctly now.

I agree. Here's the link:
subject: How to insert HTML content directly in code from JEditorPane ?