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

BadLocationException thrown while backspacing if text is wrapped in JTextPane

XMLWordPrintable

    • b01
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Reproduceable 100% of the time on Windows 10, no other OS tested.

      A DESCRIPTION OF THE PROBLEM :
      The following exception is thrown when text is wrapped in a JTextPane involving certain html tags such as <span> and <div>: javax.swing.text.StateInvariantError: GlyphView: Stale view: javax.swing.text.BadLocationException: Invalid location

      The text may be wrapped due to the size of the container, or because of a width value being set in the html as seen in the example below with div. Backspacing the text from the end the caret incorrectly located as seen in several of the examples below.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a JTextPane and set it to use text/html
      2. Set the text of the JTextPane to one of the following examples:
      // exception example 1 with span tag, window size wraps the text, press backspace 6 times
      //editor.setText("<html><body><p>abcd efgh ijkl mnop qrst</p><span>ABC</span></body></html>");

      // exception example 2 with div tag, window size wraps the text, press backspace 6 times
      //editor.setText("<div>\r\n" + "abcd efgh ijkl mnop qrst</div><p align=\"left\" style=\"margin-top: 0\">ABC</p>");

      // exception example 3, wrapped via div rather than window size, press backspace 6 times
      myFrame.setSize(500, 500);
      editor.setText("<div style=\"width: 70px\">abcd efgh ijkl mnop qrst</div><p>ABC</p>");

      // caret issue example 1 with div tag, same html as exception example 2 but window size does not force wrap, no exception thrown
      // press backspace 10 times, type asdf, press backspace more, see that the caret is incorrectly located
      //myFrame.setSize(500,500);
      //editor.setText("<div>\r\n" + "abcd efgh ijkl mnop qrst</div><p align=\"left\" style=\"margin-top: 0\">ABC</p>");

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The caret should track backspacing correctly with the provided html examples and should not throw an exception.
      ACTUAL -
      Exception in thread "AWT-EventQueue-0" javax.swing.text.StateInvariantError: GlyphView: Stale view: javax.swing.text.BadLocationException: Invalid location
      at java.desktop/javax.swing.text.GlyphView.getText(GlyphView.java:138)
      at java.desktop/javax.swing.text.GlyphPainter1.paint(GlyphPainter1.java:118)
      at java.desktop/javax.swing.text.GlyphView.paintTextUsingColor(GlyphView.java:484)
      at java.desktop/javax.swing.text.GlyphView.paint(GlyphView.java:475)
      at java.desktop/javax.swing.text.BoxView.paintChild(BoxView.java:162)
      at java.desktop/javax.swing.text.BoxView.paint(BoxView.java:434)
      at java.desktop/javax.swing.text.BoxView.paintChild(BoxView.java:162)
      at java.desktop/javax.swing.text.BoxView.paint(BoxView.java:434)
      at java.desktop/javax.swing.text.ParagraphView.paint(ParagraphView.java:573)
      at java.desktop/javax.swing.text.html.ParagraphView.paint(ParagraphView.java:238)
      at java.desktop/javax.swing.text.BoxView.paintChild(BoxView.java:162)
      at java.desktop/javax.swing.text.BoxView.paint(BoxView.java:434)
      at java.desktop/javax.swing.text.html.BlockView.paint(BlockView.java:282)
      at java.desktop/javax.swing.text.BoxView.paintChild(BoxView.java:162)
      at java.desktop/javax.swing.text.BoxView.paint(BoxView.java:434)
      at java.desktop/javax.swing.text.html.BlockView.paint(BlockView.java:282)
      at java.desktop/javax.swing.text.BoxView.paintChild(BoxView.java:162)
      at java.desktop/javax.swing.text.BoxView.paint(BoxView.java:434)
      at java.desktop/javax.swing.text.html.BlockView.paint(BlockView.java:282)
      at java.desktop/javax.swing.plaf.basic.BasicTextUI$RootView.paint(BasicTextUI.java:1538)
      at java.desktop/javax.swing.plaf.basic.BasicTextUI.paintSafely(BasicTextUI.java:757)
      at java.desktop/javax.swing.plaf.basic.BasicTextUI.paint(BasicTextUI.java:915)
      at java.desktop/javax.swing.plaf.basic.BasicTextUI.update(BasicTextUI.java:894)
      at java.desktop/javax.swing.JComponent.paintComponent(JComponent.java:842)
      at java.desktop/javax.swing.JComponent.paint(JComponent.java:1119)
      at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5311)
      at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1657)
      at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1632)
      at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1570)
      at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1337)
      at java.desktop/javax.swing.JComponent._paintImmediately(JComponent.java:5259)
      at java.desktop/javax.swing.JComponent.paintImmediately(JComponent.java:5069)
      at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:879)
      at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:862)
      at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
      at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:862)
      at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:835)
      at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:784)
      at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1898)
      at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
      at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
      at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
      at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
      at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
      at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
      at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
      at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
      at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
      at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
      at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
      at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
      at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;

      import javax.swing.JFrame;
      import javax.swing.JTextPane;
      import javax.swing.WindowConstants;

      public class TestJTextPaneGlyphEx
      {
      TestJTextPaneGlyphEx()
      {
      JFrame myFrame = new JFrame();
      myFrame.setSize(50, 500);
      myFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

      JTextPane editor = new JTextPane();
      editor.setContentType("text/html");

      // exception example 1 with span tag, window size wraps the text, press backspace 6 times
      editor.setText("<html><body><p>abcd efgh ijkl mnop qrst</p><span>ABC</span></body></html>");

      // exception example 2 with div tag, window size wraps the text, press backspace 6 times
      //editor.setText("<div>\r\n" + "abcd efgh ijkl mnop qrst</div><p align=\"left\" style=\"margin-top: 0\">ABC</p>");

      // exception example 3, wrapped via div rather than window size, press backspace 6 times
      //myFrame.setSize(500, 500);
      //editor.setText("<div style=\"width: 70px\">abcd efgh ijkl mnop qrst</div><p>ABC</p>");

      // caret issue example 1 with div tag, same html as exception example 2 but window size does not force wrap, no exception thrown
      // press backspace 10 times, type asdf, press backspace more, see that the caret is incorrectly located
      //myFrame.setSize(500,500);
      //editor.setText("<div>\r\n" + "abcd efgh ijkl mnop qrst</div><p align=\"left\" style=\"margin-top: 0\">ABC</p>");

      myFrame.add(editor, BorderLayout.CENTER);
      myFrame.setVisible(true);

      }

      public static void main(String[] args)
      {
      new TestJTextPaneGlyphEx();
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Wrapping a <span> in <p> seems to avoid the exception and caret issues. This workaround does not work for div however if a width is specified causing the text to be wrapped.

      FREQUENCY : always


            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: