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

Problem with JEditorPane to display an HTML page

1 view
Skip to first unread message

J.W.F. Thirion

unread,
Nov 5, 2002, 3:10:49 AM11/5/02
to
Hello everyone,

I am trying to display an HTML page (which is contained in a String):

String theHTML;

I currently do the following:

infoPane.setEditable(false);
infoPane.setContentType("text/html");
infoView = new JScrollPane(infoPane);

where infoPane is of type JEditorPane and infoView is JScrollPane(infoPane).
I then sit in a loop and read an XML packet over a socket, parse it, and
convert the data to HTML and store it in the theHTML String. This is thus
HTML I want to display in infoPane. I do it as follows:

try {
infoPane.read(new StringReader(tempHTML), null);
} catch (IOException e) {
System.out.println("Unable to display HTML: " + e + ".");
}

It seems to display the page (I don't get the error message in the catch
block), but I do get the following errors (sometimes) in the Java console
window:

java.lang.ArrayIndexOutOfBoundsException: 0
at javax.swing.text.html.BlockView.layoutMinorAxis(BlockView.java:240)
at javax.swing.text.BoxView.setSpanOnAxis(BoxView.java:326)
at javax.swing.text.BoxView.layout(BoxView.java:682)
at javax.swing.text.BoxView.setSize(BoxView.java:379)
at
javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1598)
at javax.swing.plaf.basic.BasicTextUI.viewToModel(BasicTextUI.java:988)
at
javax.swing.text.html.HTMLEditorKit$LinkController.mouseMoved(HTMLEditorKit.
java:625)
etc...

Does anyone know what could be wrong? I am using J2SDK1.4.1_01 under
Windows 2000.

Thanks in advance,
Derik

Christian Kaufhold

unread,
Nov 5, 2002, 1:04:57 PM11/5/02
to
Hello!

J.W.F. Thirion <de...@molo.co.za> wrote:

> I currently do the following:
>
> infoPane.setEditable(false);
> infoPane.setContentType("text/html");
> infoView = new JScrollPane(infoPane);
>
> where infoPane is of type JEditorPane and infoView is JScrollPane(infoPane).
> I then sit in a loop and read an XML packet over a socket, parse it, and
> convert the data to HTML and store it in the theHTML String. This is thus
> HTML I want to display in infoPane. I do it as follows:
>
> try {
> infoPane.read(new StringReader(tempHTML), null);
> } catch (IOException e) {
> System.out.println("Unable to display HTML: " + e + ".");
> }

[Exceptions follow]

Is JTextComponent.read(Reader, Object) documented to be thread-safe?
It is not, and I don't see any lock-acquring in the source.

Try

EditorKit k = infoPane.getUI().getEditorKit(infoPane);
final Document d = k.createDefaultDocument();

k.read(new StringReader(tempHTML), d, 0);

SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
infoPane.setDocument(d);
}
});

instead (which is functionally the same, only setDocument is done in teh
event dispatch thread).
(or use setText (which reads into the same Document, which may cause
problems with the old content's relics))


Christian

J.W.F. Thirion

unread,
Nov 6, 2002, 2:22:38 AM11/6/02
to
Dear Christian,

Thanks. I tried it and it worked very nicely.
I didn't really keep the thread safe isue in mind. That will teach me!

Again, thanks for your help.

Cheers,
Derik

"Christian Kaufhold" <use...@chka.de> wrote in message
news:2t3dc8071...@simia.chka.de...

0 new messages