Get selected text from JEditorpane as HTML

  • Thread starter Gerrit Hulleman
  • Start date
G

Gerrit Hulleman

Is it possible to get the selected text as HTML from the pane? the
getSelectedText seems unable to do this.

This is required for a customized copy & paste action. The implemented copy
function interprets the </P></P> as a new line (/n), while <BR> tags are
replaced by a space.
Also, pasting text copied from the JEditorpane is inserted as
"</P>"+clipboardcontent+"<P>".

According to current html standards, <BR> tags are considered wrong. It
should be <BR/>. Anyway to make this clear to the JEditorpane?
 
C

Chris Smith

Gerrit said:
Is it possible to get the selected text as HTML from the pane? the
getSelectedText seems unable to do this.

Right, you need EditorKit.write(Writer, Document, int, int). The last
two parameters are the starting position and length, which you can get
from the selection via methods on JEditorPane. The Writer can be a
StringWriter, so you don't necessarily need to save to disk.

The result will be a complete HTML document that contains the selected
bit of text; meaning that it will include tags like <html> and <body>.
No easy way to avoid that (though I have resorted in the past to writing
a custom HTML parser to eliminate those tags and return just a document
fragment).
According to current html standards, <BR> tags are considered wrong. It
should be <BR/>. Anyway to make this clear to the JEditorpane?

JEditorPane implements HTML 3.2, and <BR> is just fine. The only
standard where it's not fine is XHTML, and in that case <BR/> is not
okay either since XHTML is case sensitive. Perhaps you're thinking of
<br/>.

If you want to piece together bits of XHTML, it can be done by providing
a customized HTMLWriter. If you want to implement the entire XHTML
standard, then you've got a lot of work ahead of you.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top