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 matching update for my JDK. I compile the above test applet using simply: "javac HelloWorldApplet.java"
The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly.
JLabel is a class of java Swing . JLabel is used to display a short string or an image icon. JLabel can display text, image or both . JLabel is only a display of text or image and it cannot get focus . JLabel is inactive to input events such a mouse focus or keyboard focus.
JComponent is a direct subclass of javax. awt. Container, which provides methods to add child components. While this makes sense for some components, like JTable or JTree, it doesn't seem to make sense for JLabel, for example.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With