Name: jk109818 Date: 03/14/2002
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
FULL OPERATING SYSTEM VERSION :
Red Hat Linux release 7.2 (Enigma)
glibc-2.2.4-19.3
Linux 2.4.7-10 #1 Thu Sep 6 17:27:27 EDT 2001 i686 unknown
A DESCRIPTION OF THE PROBLEM :
It must be possible to disable HTML rendering of JLabels!
All Strings that are passed to JLabel.setText() and do
begin with "<html>" are always parsed as HTML documents.
This might cause exceptions to be thrown deep in Swing since
the HTML might not be of a valid structure supported by
the Swing HTML parser.
Also, the parsing of the HTML code is an expensive operation
that I would like to be able to disable.
We develop a Swing application that is based on a JTable
using JLabels as cell renderers. The text that is displayed
in the cells are fetched from a database and might contain
invalid HTML code.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a JLabel
2. Call setText() and add some malformed HTML code
3. Watch the stack trace thrown by Swing
EXPECTED VERSUS ACTUAL BEHAVIOR :
The label should get the new text without any
additional and expensive parsing of the content.
Now I get an error from Swing.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
21:15 % > java kalle
Exception in thread "main" java.lang.ClassCastException: javax.swing.JLabel
at javax.swing.text.html.EditableView.setParent(EditableView.java:91)
at javax.swing.text.CompositeView.replace(CompositeView.java:197)
at javax.swing.text.CompositeView.loadChildren(CompositeView.java:100)
at javax.swing.text.CompositeView.setParent(CompositeView.java:125)
at javax.swing.text.FlowView.loadChildren(FlowView.java:124)
at javax.swing.text.CompositeView.setParent(CompositeView.java:125)
at javax.swing.text.html.ParagraphView.setParent(ParagraphView.java:61)
at javax.swing.text.CompositeView.replace(CompositeView.java:197)
at javax.swing.text.BoxView.replace(BoxView.java:121)
at javax.swing.text.CompositeView.loadChildren(CompositeView.java:100)
at javax.swing.text.CompositeView.setParent(CompositeView.java:125)
at javax.swing.text.html.BlockView.setParent(BlockView.java:58)
at javax.swing.text.CompositeView.replace(CompositeView.java:197)
at javax.swing.text.BoxView.replace(BoxView.java:121)
at javax.swing.text.CompositeView.loadChildren(CompositeView.java:100)
at javax.swing.text.CompositeView.setParent(CompositeView.java:125)
at javax.swing.text.html.BlockView.setParent(BlockView.java:58)
at javax.swing.plaf.basic.BasicHTML$Renderer.<init>(BasicHTML.java:231)
at javax.swing.plaf.basic.BasicHTML.createHTMLView(BasicHTML.java:50)
at javax.swing.plaf.basic.BasicHTML.updateRenderer(BasicHTML.java:83)
at javax.swing.plaf.basic.BasicLabelUI.propertyChange(BasicLabelUI.java:381)
at
javax.swing.event.SwingPropertyChangeSupport.firePropertyChange(SwingPropertyChangeSupport.java:161)
at
javax.swing.event.SwingPropertyChangeSupport.firePropertyChange(SwingPropertyChangeSupport.java:130)
at javax.swing.JComponent.firePropertyChange(JComponent.java:3208)
at javax.swing.JLabel.setText(JLabel.java:302)
at kalle.main(kalle.java:10)
21:15 % >
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
The HTML code in the example is invalid since the <head></head> block is
after the <body> tag. This causes the Swing HTML parser to throw an exception.
---------
import javax.swing.*;
public class kalle {
public static void main(String args[]) {
JFrame frame = new JFrame();
JLabel test = new JLabel();
test.putClientProperty("html", null);
test.setText("<html>"+
" <body>"+
" <head>"+
" <title></title>"+
" </head>"+
" This is a dummy text"+
" </body>" +
"</html>");
frame.getContentPane().add(test);
frame.pack();
frame.setVisible(true);
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
A quick search at the internet gives workarounds
such as adding a whitespace as the first character...
(Review ID: 144131)
======================================================================