JDK-8034955 : JLabel/JToolTip throw ClassCastException for "a" </div> <div class="section row"> <div class="subsection col-md-4"> <!-- text block --> <ul class="detailsTable"> <li><span class="boldText">Type:</span> <span class="detailsText">Bug</span></li> <!-- Backport Need to Include --> <li><span class="boldText">Component:</span> <span class="detailsText">client-libs</span></li> <li><span class="boldText">Sub-Component:</span> <span class="detailsText">javax.swing</span> </li> <li><span class="boldText">Affected Version:</span> <span class="detailsText">6u37,7,8,9 </span></li> </ul> </div> <div class="subsection col-md-4"> <!-- text block --> <ul class="detailsTable"> <li><span class="boldText">Priority:</span> <span class="detailsText">P3</span></li> <li><span class="boldText">Status:</span> <span class="detailsText">Resolved</span></li> <li><span class="boldText">Resolution:</span> <span class="detailsText">Fixed</span></li> <li><span class="boldText">OS:</span> <span class="detailsText">generic</span></li> </li> </ul> </div> <div class="subsection col-md-4"> <!-- text block --> <ul class="detailsTable"> <li><span class="boldText">Submitted:</span> <span class="detailsText">2013-04-04</span> <li><span class="boldText">Updated:</span> <span class="detailsText">2014-07-29</span> <li><span class="boldText">Resolved:</span> <span class="detailsText">2014-04-01</span> </li> </ul> </div> </div> <!-- ppppp --> <div class="sectionTitle"> <label> Versions (Unresolved/Resolved/Fixed)</label> <div class="bscTooltip"> <span class="glyphicon glyphicon-info-sign versionInfoTooltip"></span> <span class="bscTooltiptext" style="width: 350px !important;">The Version table provides details related to the release that this issue/RFE will be addressed.<br><br> <strong>Unresolved</strong> : Release in which this issue/RFE will be addressed.<br><strong>Resolved</strong>: Release in which this issue/RFE has been resolved.<br> <strong>Fixed</strong> : 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. <br><br> To download the current JDK release, click <a href="https://www.oracle.com/technetwork/java/javase/downloads/index.html" target="_blank">here</a>. </span> </div> </div> <div class="section row"> <div class="subsection col-md-3"> <!-- text block --> <table class="versionTable"> <thead class="versionTableHeader"> <tr> <th>JDK 8</th> <th>JDK 9</th> </tr> </thead> <tbody> <tr> <td ><span class="versionTableVal">8u20 b09</span><span class="label round label-success">Fixed </span><a href="https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html?msgid=bugsview8" target="_blank"> <img src="/bugdatabase/bugdatabase-theme/images/download.gif"/></a></td> <td ><span class="versionTableVal">9</span><span class="label round label-success">Fixed </span><a href="https://www.oracle.com/java/technologies/javase-jdk15-downloads.html?msgid=bugsview15" target="_blank"> <img src="/bugdatabase/bugdatabase-theme/images/download.gif"/></a></td> </tr> </tbody> </table> </div> </div> <div class="sectionTitle">Related Reports</div> <table> <tr> <td> <div class="boldText" >Relates :  </div> </td> <td align="left"> <div class="bugLink" ><a href="view_bug?bug_id=6618964">JDK-6618964 - HTML rendering in JLabel throws ClassCastException </a></div> </td> </tr> </table> <div class="sectionTitle">Description</div> <div class="section row"> <div class="subsection col-md-12"><div class="descriptionText"><span id="linkDescriptionText"><PRE >FULL PRODUCT VERSION : java version " 1.7.0_09 " , java version " 1.6.0_37 " ADDITIONAL OS VERSION INFORMATION : not OS specific A DESCRIPTION OF THE PROBLEM : Setting the text of a JLabel or the tip text of a JToolTip to " <html>a<title> " (and similar text with <title> *outside* <head>...</head>) throws ClassCastException: Seems to be related to bugs 4845362 and 4908714. STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : Create a JLabel, set its text to " <html>a<title> " , display the label in a Swing GUI. Alternatively, set the tool tip text for any JComponent to " <html>a<title> " and wait for ToolTipManager to display the tip. See also the test case below. EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - JLabel/JToolTip renders plain text, either " a " or " <html>a<title> " . ACTUAL - Program exits with ClassCastException, see below. ERROR MESSAGES/STACK TRACES THAT OCCUR : 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:59) at javax.swing.text.BoxView.paintChild(BoxView.java:144) at javax.swing.text.BoxView.paint(BoxView.java:416) at javax.swing.text.BoxView.paintChild(BoxView.java:144) at javax.swing.text.BoxView.paint(BoxView.java:416) at javax.swing.text.ParagraphView.paint(ParagraphView.java:587) at javax.swing.text.html.ParagraphView.paint(ParagraphView.java:224) at javax.swing.text.BoxView.paintChild(BoxView.java:144) at javax.swing.text.BoxView.paint(BoxView.java:416) at javax.swing.text.html.BlockView.paint(BlockView.java:265) at javax.swing.text.BoxView.paintChild(BoxView.java:144) at javax.swing.text.BoxView.paint(BoxView.java:416) at javax.swing.text.html.BlockView.paint(BlockView.java:265) at javax.swing.plaf.basic.BasicHTML$Renderer.paint(BasicHTML.java:464) at javax.swing.plaf.basic.BasicLabelUI.paint(BasicLabelUI.java:156) at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143) at javax.swing.JComponent.paintComponent(JComponent.java:752) at javax.swing.JComponent.paint(JComponent.java:1029) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paint(JComponent.java:1038) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paint(JComponent.java:1038) at javax.swing.JLayeredPane.paint(JLayeredPane.java:567) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278) at javax.swing.RepaintManager.paint(RepaintManager.java:1224) at javax.swing.JComponent.paint(JComponent.java:1015) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97) at java.awt.Container.paint(Container.java:1780) at java.awt.Window.paint(Window.java:3375) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) REPRODUCIBILITY : This bug can be reproduced always. ---------- BEGIN SOURCE ---------- package com.yworks.swing; import java.awt.EventQueue; import java.util.Locale; import javax.swing.JFrame; import javax.swing.JLabel; public class HtmlRenderingTest { public static void main( String[] args ) { EventQueue.invokeLater(new Runnable() { public void run() { Locale.setDefault(Locale.ENGLISH); final JFrame frame = new JFrame(HtmlRenderingTest.class.getName()); frame.getContentPane().add(new JLabel( " <html>a<title> " )); frame.setVisible(true); } }); } } ---------- END SOURCE ---------- CUSTOMER SUBMITTED WORKAROUND : Subclass JLabel and overwrite methods setText and paintComponent: class SafeLabel extends JLabel { public void setText( final String text ) { try { super.setText(text); } catch (ClassCastException e) { if (isHtmlString(getText())) { // JDK 1.4/1.5 throw CCE here for invalid HTML // ignore - seems to render some text in spite of the exception } else { throw e; } } } protected void paintComponent( final Graphics g ) { try { super.paintComponent(g); } catch (ClassCastException e) { if (isHtmlString(getText())) { // JDK 1.6/1.7 throw CCE here for invalid HTML // ignore - seems to render some text in spite of the exception } else { throw e; } } } public JToolTip createToolTip() { final JToolTip tip = new SafeToolTip(); tip.setComponent(this); return tip; } private static boolean isHtmlString( final String text ) { return SafeToolTip.isHtmlString(text); } } class SafeToolTip extends JToolTip { public void setTipText( final String tipText ) { try { super.setTipText(tipText); } catch (ClassCastException e) { if (isHtmlString(tipText)) { // JDK 1.4/1.5 throw CCE here for invalid HTML // ignore - seems to render some text in spite of the exception } else { throw e; } } } protected void paintComponent( final Graphics g ) { try { super.paintComponent(g); } catch (ClassCastException e) { if (isHtmlString(getTipText())) { // JDK 1.6/1.7 throw CCE here for invalid HTML // ignore - seems to render some text in spite of the exception } else { throw e; } } } static boolean isHtmlString( final String text ) { return BasicHTML.isHTMLString(text); } } </PRE></span></div></div></div> <div class="sectionTitle">Comments</div> <div class="section row"> <div class="subsection col-md-12"> <table border="0" cellpadding="0" cellspacing="0" align="TOP" width="100%"> <tr><td valign="top" align="left"> <table align="left" bgcolor="#ffffff" border="0" cellpadding="5" cellspacing="2" width="100%"> <tbody> <tr><td align="left" width="80" class="grey5"> <div class="descriptionText"><span id="linkCommentText">There is missed 'instance of' check in the EditableView.paint(Graphics. shape) method.</span></div> </td></tr> <tr><td align="right" width="80" class="grey5"> <div class="commentDate"> 03-03-2014 </div><hr> </td></tr> <tr><td align="left" width="80" class="grey5"> <div class="descriptionText"><span id="linkCommentText">Release team: Approved for deferral.</span></div> </td></tr> <tr><td align="right" width="80" class="grey5"> <div class="commentDate"> 18-02-2014 </div><hr> </td></tr> <tr><td align="left" width="80" class="grey5"> <div class="descriptionText"><span id="linkCommentText">8-defer-request: Not a regression from JDK 7, not a JCK, not a P1. Not a stopper for JDK 8.</span></div> </td></tr> <tr><td align="right" width="80" class="grey5"> <div class="commentDate"> 14-02-2014 </div><hr> </td></tr> <tr><td align="left" width="80" class="grey5"> <div class="descriptionText"><span id="linkCommentText">This is the corrected source that does not have spaces in the label text (probably because of the issue INTJDK-7605718 ) ---------- BEGIN SOURCE ---------- package com.yworks.swing; import java.awt.EventQueue; import java.util.Locale; import javax.swing.JFrame; import javax.swing.JLabel; public class HtmlRenderingTest { public static void main( String[] args ) { EventQueue.invokeLater(new Runnable() { public void run() { Locale.setDefault(Locale.ENGLISH); final JFrame frame = new JFrame(HtmlRenderingTest.class.getName()); frame.getContentPane().add(new JLabel( "<html>a<title>")); frame.setVisible(true); } }); } } ---------- END SOURCE ----------</span></div> </td></tr> <tr><td align="right" width="80" class="grey5"> <div class="commentDate"> 14-02-2014 </div><hr> </td></tr> </tbody></table> </td></tr> </table> </div> </div> <br/><br/> <!-- pppppppp --> </div> <style> /*CSS for Social-icons*/ .footer-icon, .footer-icon ul { text-align: right; padding-right: 0px; margin-right: 0px; } .footer-icon ul li { list-style: none; display: inline; } .footer-icon img.social-icon { width: 17px; height: 16px; margin-left: 4px; background-image: url(/bugdatabase/bugdatabase-theme/images/footer/social-icons.png); background-repeat: no-repeat; background-color: transparent; } .footer-icon img.social-icon.facebook { background-position: -163px 0; } .footer-icon img.social-icon.facebook:hover { background-position: -183px 0; } .footer-icon img.social-icon.linkedin { background-position: -203px 0; } .footer-icon img.social-icon.linkedin:hover { background-position: -223px 0; } .footer-icon img.social-icon.twitter { background-position: -243px 0; } .footer-icon img.social-icon.twitter:hover { background-position: -263px 0; } .footer-icon img.social-icon.googleplus { background-position: -43px 0; } .footer-icon img.social-icon.googleplus:hover { background-position: -63px 0; } .footer-icon img.social-icon.youtube { background-position: -123px 0; } .footer-icon img.social-icon.youtube:hover { background-position: -143px 0; } .footer-icon img.social-icon.rss { background-position: -283px 0; } .footer-icon img.social-icon.rss:hover { background-position: -303px 0; } </style> <footer class="footer"> <div class="container"> <div class="col-md-12"> <!--Footer Row--> <div class="row"> <h1 class="oracle-tag"> <a href="https://www.oracle.com/us/corporate/index.html"><span><img src="/bugdatabase/bugdatabase-theme/images/footer/oracle-tagline.png" /></span></a> </h1> <!--Column-1--> <div class="col-md-8 footer-link"> <ul> <li><a href="https://www.oracle.com/us/syndication/subscribe/index.html" target="_blank">Subscribe</a></li> <li><a href="https://www.oracle.com/us/corporate/index.html" target="_blank">About Oracle</a></li> <li><a href="https://www.oracle.com/us/corporate/careers/index.html" target="_blank">Careers</a></li> <li><a href="https://www.oracle.com/us/corporate/contact/index.html" target="_blank">Contact Us</a></li> <li><a href="https://www.oracle.com/us/sitemaps/index.html" target="_blank">Site Maps</a></li> <li><a href="https://www.oracle.com/us/legal/index.html" target="_blank">Legal Notices</a></li> <li><a href="https://www.oracle.com/us/legal/terms/index.html" target="_blank">Terms of Use</a></li> <li><a href="https://www.oracle.com/us/legal/privacy/index.html" target="_blank">Your Privacy Rights</a></li> <li><span id='teconsent'></span></li> </ul> </div> <!--Column-2--> <div class="col-md-4 footer-icon"> <ul> <li><a href="https://www.oracle.com/us/social-media/facebook/index.html" title="Oracle on Facebook" target="_blank"><img alt="Facebook" border="0" class="social-icon facebook" src="/bugdatabase/bugdatabase-theme/images/footer/spacer.gif"></a> </li> <li><a data-lbl="linkedin" href="https://www.oracle.com/us/social-media/linkedin/index.html" target="_blank"><img alt="Linked In" border="0" class="social-icon linkedin" src="/bugdatabase/bugdatabase-theme/images/footer/spacer.gif"></a></li> <li><a data-lbl="twitter" href="https://www.oracle.com/us/social-media/twitter/index.html" target="_blank"><img alt="Twitter" border="0" class="social-icon twitter" src="/bugdatabase/bugdatabase-theme/images/footer/spacer.gif"></a></li> <li><a data-lbl="googleplus" href="https://plus.google.com/u/0/115607918987921226255" target="_blank"><img alt="Google+" border="0" class="social-icon googleplus" src="/bugdatabase/bugdatabase-theme/images/footer/spacer.gif"></a></li> <li><a data-lbl="youtube" href="https://www.youtube.com/oracle/" target="_blank"><img alt="YouTube" border="0" class="social-icon youtube" src="/bugdatabase/bugdatabase-theme/images/footer/spacer.gif"></a></li> <li><a data-lbl="rssfeeds" href="https://www.oracle.com/us/syndication/feeds/index.html" target="_blank"><img alt="RSS Feeds" border="0" class="social-icon rss" src="/bugdatabase/bugdatabase-theme/images/footer/spacer.gif"></a></li> </ul> </div> <!-- End of Col2 --> <!-- html for Go Top--> <div> <a id="scroll-top" class="hide" href="#"><i class="glyphicon glyphicon-chevron-up"></i></a> </div> </div> <!-- End of Col --> </div> <!-- End of Row --> </div> <!--End of class="container"--> </footer> </body>