JDK-4652898 : Impossible to disable HTML Rendering in a JLabel
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux,windows_2000
  • CPU: x86
  • Submitted: 2002-03-14
  • Updated: 2003-09-26
  • Resolved: 2003-09-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 tigerFixed
Related Reports
Duplicate :  
Description

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) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b22
14-06-2004

EVALUATION Name: anR10225 Date: 04/21/2003 'text' property for JLabel and other components could have any content (e.g. generated or obtained from DB) and if it starts with '<html>' it might not mean that text should be treated and parsed as HTML. Suggestion is to add JComponent client property 'html.disable'. If this property is set to Boolean.TRUE the component's 'text' property is never treated as HTML and is always rendered as plain text. This property should be checked by javax.swing.plaf.basic.BasicHTML.updateRenderer(). ====================================================================== Name: anR10225 Date: 09/01/2003 The client property 'html.disable' is now can be used to disable HTML rendering. It may be used with JComponent's which support HTML rendering. They are normally JLabel and AbstractButton classes and their subclasses such as: DefaultListCellRenderer DefaultTableCellRenderer DefaultTreeCellRenderer JButton JMenuItem JMenu JToggleButton JRadioButton JCheckbox ======================================================================
11-06-2004