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.
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
<FONT color=red>Hello World</FONT>
Hope this helps... thanks for any assistance.
Cliff.
"Cliff" <che...@nospamwallstreetwise.com> wrote in message
news:ZUzH7.67710$XA5.12...@typhoon.nyc.rr.com...
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
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
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...
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
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