开发者

HTML no longer working in JLabel (and other components)

I have a java applet that I've written and have been running it for quite a while. In the applet I have a bunch of JLabels that use HTML in their text content (which is allowed, and has been working for years). The main reason I use HTML is to allow line breaks in JLabels.

The issue: fairly recently the JLabels that have HTML in them (some don't) stopped displaying their text. I think it might have something to do with the recent java update (Java SE 6 Update 22, on 2010-Oct-12) not 100 percent sure, but the problems did seem to start around then. Maybe a bug has been introduced? or feature removed?

I tried with a JEditorPane and seemed to have the same issues when the content was HTML.

Also something important to note is that the first time you load the applet (first time your java runtime starts) it works FINE, but if you refresh the webpage then it has the issues as described.

Does anyone have similar issues? anyone have any insights? or am I just doing something dumb?

I made a very simple test applet and I can reproduce the issues with 100% regularity (remembering that the first time the runtime loads it will work fine, only successive refreshes will cause the issues):

[HelloWorldApplet.java]

import javax.swing.*;

public class HelloWorldApplet extends JApplet
{
    public void init()
    {
        this.add(new JLabel("Hello Everybody (Text)"), java.awt.BorderLayout.NORTH);
        this.add(new JLabel("<html><body>Hello Everybody (HTML)</body></html>"), java.awt.BorderLayout.SOUTH);
    }
}

[HellowWorldApplet.html]

<html>
<title>A Test</title>
<body>
    <applet code="HelloWorldApplet.class" width="320" height="120">
        You need Java
    </applet>
</body>
</html>

PS. I've been mainly testing using Chrome but I did breifly try in IE 8 also. Also I (obviously) have the latest Java SE 6 update 22 installed as my run time, and have the mat开发者_JAVA百科ching update for my JDK. I compile the above test applet using simply: "javac HelloWorldApplet.java"


Wasted an afternoon looking for the cause of this, see the following bug report http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6993691

The workaround in the bug report is:

import javax.swing.*;
import javax.swing.text.html.parser.ParserDelegator;

public class HelloWorldApplet extends JApplet {
    public void init() {
       ParserDelegator workaround = new ParserDelegator();
       applet.add(new JLabel("Hello Everybody (Text)"), java.awt.BorderLayout.NORTH);
       applet.add(new JLabel("<html>Hello Everybody (HTML)</html>"),  java.awt.BorderLayout.SOUTH);
    }
}


Sounds from the description like a threading issue.

Unfortunately applets are not constructed or have their lifecycle methods called on the AWT Event Dispatch Thread (EDT).

Technically your constructor and init code should be executed inside java.awt.EventQueue.invokeAndWait.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜