Inserting a span tag in JEditorpane

  • Thread starter Gerrit Hulleman
  • Start date
G

Gerrit Hulleman

Is it possible ot insert a span tag in the editorpane?

Using the code:
HTMLDocument doc = (HTMLDocument) htmledit.getEditor().getDocument();
HTMLEditorKit kit = (HTMLEditorKit) htmledit.getEditor().getEditorKit();
int start = htmledit.getEditor().getSelectionStart();
int end = htmledit.getEditor().getSelectionEnd();

The following does not work:
kit.insertHTML(doc, start, "<span
class=\""+fontfamily+"\">"+htmledit.getEditor().getSelectedText()+"</span>",
0, 0, HTML.Tag.DIV);
--> does not add a thing, does work for a div tag
kit.insertHTML(doc, start, "<span></span>", 0, 0, HTML.Tag.SPAN);
--> add only a </span>, does work for a div tag
doc.insertString(start, "<span
class=\""+fontfamily+"\">"+htmledit.getEditor().getSelectedText()+"</span>",
null); (thought it was worth a try)
--> offcourse, adds this as text (with replacements: "<" => "&lt;")
or
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
kit.write(buffer, doc, 0, start);
String newtext = buffer.toString();
System.out.println(newtext);
........
--> this code copies the text till the start of the selection, but
adds closing tags ( >< )

Is it at all possible to insert span tags? Perhaps it is possible to add
tags usingthe kit.insertHTML without telling it what kind of tag it is...

Gerrit
 
C

Chris Smith

Gerrit said:
The following does not work:
kit.insertHTML(doc, start, "<span
class=\""+fontfamily+"\">"+htmledit.getEditor().getSelectedText()+"</span>",
0, 0, HTML.Tag.DIV);

Did you mean to have HTML.Tag.DIV at the end? You're supposed to
include the outer tag there, such as HTML.Tag.SPAN. Perhaps you posted
the wrong code by accident.

In any case, the far easier way to accomplish this is to work directly
with the HTMLDocument, by saying:

MutableArributeSet attr = new SimpleAttributeSet();
attr.addAttribute(HTML.Attribute.CLASS, "...");
MutableAttributeSet outerAttr = new SimpleAttributeSet();
outerAttr.addAttribute(HTML.Tag.SPAN, attr);
doc.setCharacterAttributes(start, end - start, outerAttr, true);

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
G

Gerrit Hulleman

Was able to insert the span tag. The tag can be inserted in the beta2 1.5
jdk using the following code:
InsertHTMLTextAction action = new InsertHTMLTextAction("InsertHTML",
"<span
class='."+fontfamily+"'>"+htmledit.getEditor().getSelectedText()+"</span>",
HTML.Tag.P, HTML.Tag.SPAN, null, null );
action.actionPerformed(ae);

Oh happy days!

G

Gerrit Hulleman said:
Is it possible ot insert a span tag in the editorpane?

Using the code:
HTMLDocument doc = (HTMLDocument) htmledit.getEditor().getDocument();
HTMLEditorKit kit = (HTMLEditorKit) htmledit.getEditor().getEditorKit();
int start = htmledit.getEditor().getSelectionStart();
int end = htmledit.getEditor().getSelectionEnd();

The following does not work:
kit.insertHTML(doc, start, "<span
 

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

Forum statistics

Threads
474,002
Messages
2,570,258
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top