Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding HTML text to JEditorPane

486 views
Skip to first unread message

Cliff

unread,
Nov 11, 2001, 2:00:09 PM11/11/01
to
Just looking for the easiest way to append HTML text to an already existing
JEditorPane. The content type is "text/html". Once I set the initial text
I want to append to it. I'd rather not manage a copy of the text that is
supposed to go in the editor. Is there a way to insert additional text?

For example,

editorpane.setText("My first text string");
results in this being inserted in the document
"<HTML><HEAD TITLE etc... ><P>My first text string</P></HTML>"
now I'd like to insert another text string (using imaginary method)
editorPane.appendText("My second text string");
so the result is ...
"<HTML><HEAD TITLE etc... ><P>My first text string My second text
string</P></HTML>"

Hope that makes sense... One way I can think of is managing a copy of the
text and 'setText'ing the whole thing again but that seems like a waste of
resources and a performance problem for large documents... I'm not really
familiar with the EditorKits but there must be something in there...
pointers are greatly appreciated!

Thanks,
Cliff.

Cliff

unread,
Nov 11, 2001, 3:42:38 PM11/11/01
to
I hate to follow up my own post... but... here's more information but still
no solution to the problem. If I iterate over the elements in the Document
I can get the offsets of the body and insert text into the body. However,
the 'insertString' method translates embedded HTML by escaping the text and
any embedded HTML tags...

Example, I'm trying to insert the following into my document using the
insertString method:

<FONT color=red>Hello World</FONT>

However, this is what gets inserted

&lt;FONT color=red&gt;Hello World&lt;/FONT&gt;

Hope this helps... thanks for any assistance.

Cliff.


"Cliff" <che...@nospamwallstreetwise.com> wrote in message
news:ZUzH7.67710$XA5.12...@typhoon.nyc.rr.com...

Linda Radecke

unread,
Nov 11, 2001, 4:16:06 PM11/11/01
to

Cliff wrote:

> Just looking for the easiest way to append HTML text to an already existing
> JEditorPane. The content type is "text/html". Once I set the initial text
> I want to append to it. I'd rather not manage a copy of the text that is
> supposed to go in the editor. Is there a way to insert additional text?

To insert/append text into a Document basically you could use
void insertString (int offs, AttributeSet a) (in Document) /
void insertAfterEnd()(HTMLDocument). But HTMLEditorKit has the
method insertHTML(....) to insert additional HTML code into an
existing HTML document. You can, e.g specify a String containing
your HTML code and insert this then at the end of your existing
document with document.getLength(). as your offset.

Linda
--
_ " _ When the fantasy bells of the universe ring You can
(_\|/_) fly through the sky on a dragonfly's wing - There is
(/|\) magic within, there is magic without
Kate Bush, The Magician of Lublin


Christian Kaufhold

unread,
Nov 11, 2001, 5:17:25 PM11/11/01
to
Hello!

Cliff <che...@nospamwallstreetwise.com> wrote:

> Just looking for the easiest way to append HTML text to an already existing
> JEditorPane. The content type is "text/html". Once I set the initial text
> I want to append to it. I'd rather not manage a copy of the text that is
> supposed to go in the editor. Is there a way to insert additional text?

HTMLDocument.insert(Before|After)(Start|End)
HTMLDocument.set(Inner|Outer)HTML
HTMLEditorKit.insertHTML
HTMLEditorKit.InsertHTMLTextAction


Christian

Cliff

unread,
Nov 11, 2001, 5:37:58 PM11/11/01
to
Hi, thanks for writing back... I'm getting the following error on my
'insertBeforeEnd' of BadLocationException. Here's a code snippet. What I'm
doing is locating the body element (there's got to be a more efficient way).
Then I'm using 'insertBeforeEnd' to try and update the text...

Element body=null;

private void updateBody(String myString) {
if(body==null) // we haven't located the body element yet so find it.
{
ElementIterator i = new
ElementIterator(transcriptEditorPane.getDocument());
Element e;

while ((e = i.next()) != null) {
if (e.getAttributes().getAttribute(AttributeSet.NameAttribute)==
HTML.Tag.BODY) {
body = e;
break;
}
}
}

// sanity check code here to make sure that we have the body element
String bodyText=null;

try {

bodyText=transcriptEditorPane.getDocument().getText(body.getStartOffset(),bo
dy.getEndOffset()-body.getStartOffset());
} catch (Exception j) {
j.printStackTrace();
}
System.out.println(bodyText);
// the above works so I know I'm getting the body element

if (body != null) {
try {
// this throws BadLocationException
((HTMLDocument)editorPane.getDocument()).insertBeforeEnd(body,myString);
// line 287 in stack trace below
} catch (Exception ioe)
{
ioe.printStackTrace();
}
}
}

The stack trace is here:

javax.swing.text.BadLocationException: Invalid insert
at javax.swing.text.GapContent.insertString(GapContent.java:112)
at
javax.swing.text.DefaultStyledDocument.insert(DefaultStyledDocument.java:187
)
at javax.swing.text.html.HTMLDocument.insert(HTMLDocument.java:212)
at
javax.swing.text.html.HTMLDocument$HTMLReader.flushBuffer(HTMLDocument.java:
3014)
at
javax.swing.text.html.HTMLDocument$HTMLReader.flush(HTMLDocument.java:1921)
at
javax.swing.text.html.HTMLDocument.insertHTML(HTMLDocument.java:1027)
at
javax.swing.text.html.HTMLDocument.insertBeforeEnd(HTMLDocument.java:841)
at gui.Test.updateBody(Test.java:287)

I'm using JDK 1.3.1_01

Cliff

"Linda Radecke" <li...@jalice.ch> wrote in message
news:3BEEEA96...@jalice.ch...

Linda Radecke

unread,
Nov 11, 2001, 7:18:48 PM11/11/01
to

Cliff wrote:

> Hi, thanks for writing back... I'm getting the following error on my
> 'insertBeforeEnd' of BadLocationException. Here's a code snippet. What I'm
> doing is locating the body element (there's got to be a more efficient way).
> Then I'm using 'insertBeforeEnd' to try and update the text...

[...]

Unfortunately insertBeforeEnd() seems to be buggy, I had no
luck with it, insertAfterEnd() in turn, works fine for me, though.


Linda
--
.-. I hear him, before I go to sleep And focus on the day
( ( that's been. I realise he's there, When I turn the
'-` light off and turn over.
Kate Bush - The Man With The Child In His Eyes

Linda Radecke

unread,
Nov 11, 2001, 7:25:17 PM11/11/01
to

Cliff wrote:

> Hi, thanks for writing back... I'm getting the following error on my
> 'insertBeforeEnd' of BadLocationException. Here's a code snippet. What I'm
> doing is locating the body element (there's got to be a more efficient way).
> Then I'm using 'insertBeforeEnd' to try and update the text...

[...]

> The stack trace is here:
>
> javax.swing.text.BadLocationException: Invalid insert
> at javax.swing.text.GapContent.insertString(GapContent.java:112)
> at
> javax.swing.text.DefaultStyledDocument.insert(DefaultStyledDocument.java:187
> )

[...etc...]

Since I was wondering, I looked through the
BugParade quickly, there is a Bug reported related to that:

http://developer.java.sun.com/developer/bugParade/bugs/4496801.html

0 new messages