Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6618964

HTML rendering in JLabel throws ClassCastException

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      java version "1.6.0_03"
      Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
      Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP SP 2

      A DESCRIPTION OF THE PROBLEM :
      When including an <applet> tag without a closing tag, a ClassCastException is thrown in javax.swing.text.html.EditableView in the paint method.
      It cannot cast a JLabel to a JTextComponent.

      The problem is very difficult to trace down from application code, because the exception will not be caught by a catch clause. Apparently it originates from an invocation after the return from setVisible.

      The issue may be related to 4209280 (which is fixed) but in this case not an invalid tag is used, but a valid but unclosed tag.

      In the EditableView.paint method no check is made if the component is instanceof JTextComponent (I guess that is the cause of the problem)

      In another method, setParent, that check IS made and I think it should be checked in paint as well.

      In the source code of the upcoming Java 7.0 I see that no instanceof check is made either in the paint method.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the attached code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Corrupted text on the displayed GUI
      ACTUAL -
      At first no painting at all on the GUI.
      ClassCastException thrown
      When hiding and making visible again corrupted text accompanied by lots of ClassCastExceptions.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No error thrown
      Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JLabel cannot be cast to javax.swing.text.JTextComponent
      at javax.swing.text.html.EditableView.paint(Unknown Source)
      at javax.swing.text.BoxView.paintChild(Unknown Source)
      at javax.swing.text.BoxView.paint(Unknown Source)
      at javax.swing.text.BoxView.paintChild(Unknown Source)
      at javax.swing.text.BoxView.paint(Unknown Source)
      at javax.swing.text.ParagraphView.paint(Unknown Source)
      at javax.swing.text.html.ParagraphView.paint(Unknown Source)
      at javax.swing.text.BoxView.paintChild(Unknown Source)
      at javax.swing.text.BoxView.paint(Unknown Source)
      at javax.swing.text.html.BlockView.paint(Unknown Source)
      at javax.swing.text.BoxView.paintChild(Unknown Source)
      at javax.swing.text.BoxView.paint(Unknown Source)
      at javax.swing.text.html.BlockView.paint(Unknown Source)
      at javax.swing.plaf.basic.BasicHTML$Renderer.paint(Unknown Source)
      at javax.swing.plaf.basic.BasicLabelUI.paint(Unknown Source)
      at javax.swing.plaf.ComponentUI.update(Unknown Source)
      at javax.swing.JComponent.paintComponent(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JLayeredPane.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paintToOffscreen(Unknown Source)
      at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
      at javax.swing.RepaintManager.paint(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
      at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
      at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
      at java.awt.Container.paint(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
      at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
      at java.awt.event.InvocationEvent.dispatch(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
      Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JLabel cannot be cast to javax.swing.text.JTextComponent
      at javax.swing.text.html.EditableView.paint(Unknown Source)
      at javax.swing.text.BoxView.paintChild(Unknown Source)
      at javax.swing.text.BoxView.paint(Unknown Source)
      at javax.swing.text.BoxView.paintChild(Unknown Source)
      at javax.swing.text.BoxView.paint(Unknown Source)
      at javax.swing.text.ParagraphView.paint(Unknown Source)
      at javax.swing.text.html.ParagraphView.paint(Unknown Source)
      at javax.swing.text.BoxView.paintChild(Unknown Source)
      at javax.swing.text.BoxView.paint(Unknown Source)
      at javax.swing.text.html.BlockView.paint(Unknown Source)
      at javax.swing.text.BoxView.paintChild(Unknown Source)
      at javax.swing.text.BoxView.paint(Unknown Source)
      at javax.swing.text.html.BlockView.paint(Unknown Source)
      at javax.swing.plaf.basic.BasicHTML$Renderer.paint(Unknown Source)
      at javax.swing.plaf.basic.BasicLabelUI.paint(Unknown Source)
      at javax.swing.plaf.ComponentUI.update(Unknown Source)
      at javax.swing.JComponent.paintComponent(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintToOffscreen(Unknown Source)
      at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
      at javax.swing.RepaintManager.paint(Unknown Source)
      at javax.swing.JComponent._paintImmediately(Unknown Source)
      at javax.swing.JComponent.paintImmediately(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
      at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
      at java.awt.event.InvocationEvent.dispatch(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.awt.BorderLayout;

      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.SwingUtilities;

      public class JLabelHtml {

          private static String htmlError =
      "<html>Wrongly HTML formatted text that contains a valid begin tag <applet> but no applet end tag.</html>";

          public static void main(String[] args) {
      new JLabelHtml().test();
          }

          private void test() {
      SwingUtilities.invokeLater(new Runnable() {

      @Override
      public void run() {
      try {
      JFrame frame = new JFrame("Bug");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationByPlatform(true);
      frame.getContentPane().add(
      new JLabel(htmlError, JLabel.CENTER),
      BorderLayout.CENTER);
      frame.setSize(200, 200);
      // frame.pack();
      frame.setVisible(true);
      System.out.println("No error thrown");
      } catch (Throwable t) {
      t.printStackTrace(System.out);
      System.out.println(htmlError);
      }
      }
      });
          }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      1: do not use html with errors.

      However this is not always possible when the text originates from a source that is not under the developer's control.
      Bug Description:
      A JLabel show the label text as formated HTML if the text start with "<html>".
      A problem occur if the label text already contains a "<HTML>" tag.

      The follow label:

        new JLabel("<html><HTML><HEAD><TITLE>mytitle</TITLE></HEAD>text</HTML>");

      or the follow message box

        JOptionPane.showMessageDialog(this, "<html><HTML><HEAD><TITLE>mytitle</TITLE></HEAD>text</HTML>");

      show the follow exception.

      This occur with Java 6 and Java 7.
      A workaround is to use a third party tool like JWebEngine.

      Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JLabel cannot be cast to
      javax.swing.text.JTextComponent

               at javax.swing.text.html.EditableView.paint(EditableView.java:76)
               at javax.swing.text.BoxView.paintChild(BoxView.java:161)
               at javax.swing.text.BoxView.paint(BoxView.java:433)
               at javax.swing.text.BoxView.paintChild(BoxView.java:161)
               at javax.swing.text.BoxView.paint(BoxView.java:433)
               at javax.swing.text.ParagraphView.paint(ParagraphView.java:587)
               at javax.swing.text.html.ParagraphView.paint(ParagraphView.java:233)
               at javax.swing.text.BoxView.paintChild(BoxView.java:161)
               at javax.swing.text.BoxView.paint(BoxView.java:433)
               at javax.swing.text.html.BlockView.paint(BlockView.java:282)
               at javax.swing.text.BoxView.paintChild(BoxView.java:161)
               at javax.swing.text.BoxView.paint(BoxView.java:433)
               at javax.swing.text.html.BlockView.paint(BlockView.java:282)
               at javax.swing.plaf.basic.BasicHTML$Renderer.paint(BasicHTML.java:481)
               at javax.swing.plaf.basic.BasicLabelUI.paint(BasicLabelUI.java:173)
               at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
               at javax.swing.JComponent.paintComponent(JComponent.java:778)
               at javax.swing.JComponent.paint(JComponent.java:1054)
               at javax.swing.JComponent.paintChildren(JComponent.java:887)
               at javax.swing.JComponent.paint(JComponent.java:1063)
               at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
               at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
               at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
               at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
               at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
               at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
               at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
               at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
               at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
               at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
               at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
               at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
               at java.awt.EventQueue.access$000(EventQueue.java:101)
               at java.awt.EventQueue$3.run(EventQueue.java:666)
               at java.awt.EventQueue$3.run(EventQueue.java:664)
               at java.security.AccessController.doPrivileged(Native Method)
               at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
               at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
               at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
               at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
               at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
               at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
               at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
               at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

      Attachments

        Issue Links

          Activity

            People

              alexsch Alexandr Scherbatiy
              igor Igor Nekrestyanov (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: