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

JTextPane.insertIcon doesn't insert image in HTML docs

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • client-libs



      Name: gm110360 Date: 04/19/2002


      FULL PRODUCT VERSION :
      java version "1.4.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-rc-b91)
      Java HotSpot(TM) Client VM (build 1.4.0-rc-b91, mixed mode)


      FULL OPERATING SYSTEM VERSION :
      Windows 98 [Version 4.10.2222]




      A DESCRIPTION OF THE PROBLEM :
      The JTextPane insertIcon method doesn't insert the image if
      the JTextPane contains an HTML document. Instead of
      inserting the image, it inserts a couple inch gap at the
      insertion point. In contrast, the insertIcon method works
      fine for either regular StyledDocument or RTF documents.

      This is an improvement on the situation for 1.3.1, 1.3, and
      1.2.2. Under those versions the same code throws:
      java.lang.ArrayIndexOutOfBoundsException: -1 < 0

      This was first reported as bug report 4240377 for the Swing
      classes under JDK 1.1. The bug was closed as "not
      reproducible". Three subsequent posting under that bug
      report thread indicated that it was still a bug in 1.2.2 and
      1.3.1, but it does not appear the bug was ever reopened.

      The code provided below will generate the
      ArrayIndexOutOfBoundsException on 1.2.2, 1.3, and 1.3.1 and
      is completely reproducible. The same code under 1.4rc no
      longer blows up but it still doesn't insert the image. The
      code includes a one line change to switch from an HTML
      document to a regular StyledDocument which works fine.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile and run the code below
      2. Make sure you have a live network connection since it
      fetches the image from the Java web site. Problem still
      occurs if you use a local image file:
         new IconImage("image.gif");
      3. Replace the "doesn't work" line with the "does work" line
      to verify that it works when the document isn't HTML

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      1.4rc1: a couple inch gap inserted at the insertion point
      (which appears to be one character not multiple spaces)

      1.3.1, 1.3, 1.2.2:
      java.lang.ArrayIndexOutOfBoundsException: -1 < 0


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Here is the stack trace under 1.3.1 (1.3 and 1.2.2 are similar):

      Exception occurred during event dispatching:
      java.lang.ArrayIndexOutOfBoundsException
          at javax.swing.text.CompositeView.getView(CompositeView.java:146)
          at javax.swing.text.FlowView$FlowStrategy.createView(FlowView.java:591)
          at javax.swing.text.FlowView$FlowStrategy.layoutRow(FlowView.java:466)
          at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:423)
          at javax.swing.text.FlowView.layout(FlowView.java:182)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at javax.swing.text.BoxView.layout(BoxView.java:600)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at javax.swing.text.BoxView.layout(BoxView.java:600)
          at javax.swing.text.FlowView.layout(FlowView.java:191)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at javax.swing.text.BoxView.layout(BoxView.java:600)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at javax.swing.text.BoxView.layout(BoxView.java:600)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at
      javax.swing.plaf.basic.BasicTextUI$RootView.setSize(BasicTextUI.java:1450)
          at javax.swing.plaf.basic.BasicTextUI.modelToView(BasicTextUI.java:817)
          at javax.swing.plaf.basic.BasicTextUI.modelToView(BasicTextUI.java:793)
          at javax.swing.text.JTextComponent.modelToView(JTextComponent.java:882)
          at javax.swing.text.DefaultCaret.setVisible(DefaultCaret.java:615)
          at javax.swing.text.DefaultCaret.focusGained(DefaultCaret.java:252)
          at
      javax.swing.text.DefaultCaret$FocusHandler.focusGained(DefaultCaret.java:1049)
          at java.awt.AWTEventMulticaster.focusGained(AWTEventMulticaster.java:161)
          at java.awt.Component.processFocusEvent(Component.java:3639)
          at javax.swing.JComponent.processFocusEvent(JComponent.java:1980)
          at java.awt.Component.processEvent(Component.java:3535)
          at java.awt.Container.processEvent(Container.java:1164)
          at java.awt.Component.dispatchEventImpl(Component.java:2593)
          at java.awt.Container.dispatchEventImpl(Container.java:1213)
          at java.awt.Component.dispatchEvent(Component.java:2497)
          at java.awt.LightweightDispatcher.processFocusEvent(Container.java:2167)
          at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2130)
          at java.awt.Container.dispatchEventImpl(Container.java:1200)
          at java.awt.Window.dispatchEventImpl(Window.java:926)
          at java.awt.Component.dispatchEvent(Component.java:2497)
          at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
          at
      java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:1
      31)
          at
      java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
          at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)
      Exception occurred during event dispatching:
      java.lang.ArrayIndexOutOfBoundsException
          at javax.swing.text.CompositeView.getView(CompositeView.java:146)
          at javax.swing.text.FlowView$FlowStrategy.createView(FlowView.java:591)
          at javax.swing.text.FlowView$FlowStrategy.layoutRow(FlowView.java:466)
          at javax.swing.text.FlowView$FlowStrategy.layout(FlowView.java:423)
          at javax.swing.text.FlowView.layout(FlowView.java:182)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at javax.swing.text.BoxView.layout(BoxView.java:600)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at javax.swing.text.BoxView.layout(BoxView.java:600)
          at javax.swing.text.FlowView.layout(FlowView.java:191)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at javax.swing.text.BoxView.layout(BoxView.java:600)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at javax.swing.text.BoxView.layout(BoxView.java:600)
          at javax.swing.text.BoxView.setSize(BoxView.java:265)
          at javax.swing.plaf.basic.BasicTextUI$RootView.paint(BasicTextUI.java:1169)
          at javax.swing.plaf.basic.BasicTextUI.paintSafely(BasicTextUI.java:523)
          at javax.swing.plaf.basic.BasicTextUI.paint(BasicTextUI.java:657)
          at javax.swing.plaf.basic.BasicTextUI.update(BasicTextUI.java:636)
          at javax.swing.JComponent.paintComponent(JComponent.java:395)
          at javax.swing.JComponent.paint(JComponent.java:687)
          at javax.swing.JComponent.paintChildren(JComponent.java:498)
          at javax.swing.JComponent.paint(JComponent.java:696)
          at javax.swing.JComponent.paintChildren(JComponent.java:498)
          at javax.swing.JComponent.paint(JComponent.java:696)
          at javax.swing.JLayeredPane.paint(JLayeredPane.java:546)
          at javax.swing.JComponent.paintChildren(JComponent.java:498)
          at javax.swing.JComponent.paint(JComponent.java:669)
          at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:23)
          at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:54)
          at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:91)
          at java.awt.Container.paint(Container.java:960)
          at sun.awt.RepaintArea.paint(RepaintArea.java:298)
          at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:196)
          at java.awt.Component.dispatchEventImpl(Component.java:2663)
          at java.awt.Container.dispatchEventImpl(Container.java:1213)
          at java.awt.Window.dispatchEventImpl(Window.java:926)
          at java.awt.Component.dispatchEvent(Component.java:2497)
          at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
          at
      java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:1
      31)
          at
      java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)
          at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
          at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)


      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.*;
      import java.net.*;
      import javax.swing.*;
      import javax.swing.text.*;
      import javax.swing.text.html.*;

      class Test {
          static public void main(String args[]){
              JFrame frame = new JFrame("Test");
              frame.setSize(450,200);
                      
              JTextPane pane = new JTextPane();
              frame.getContentPane().add(pane);

              // doesn't work:
              setHTML(pane, "<html>This shows that insertIcon<br>does <b>not</b> work
      in HTML documents</html>");

              // does work:
              //pane.replaceSelection("This shows that insertIcon works in a regular
      StyledDocument");
              
              try {
                  URL url = new
      URL("http://java.sun.com/images/logos/javalogo52x88.gif");
                  pane.setCaretPosition(11);
                  pane.insertIcon(new ImageIcon(url));
              } catch (MalformedURLException e) {}

              frame.setVisible(true);

              frame.addWindowListener( new java.awt.event.WindowAdapter() {
                  public void windowClosing(java.awt.event.WindowEvent e)
                       {System.exit(0); } } );

         }

          static void setHTML(JTextPane pane, String str) {
      HTMLEditorKit kit = new HTMLEditorKit();
              HTMLDocument doc = null;
              try {
                  doc = new HTMLDocument();
                  kit.read(new StringReader(str),doc,0);
              } catch(Exception e) { }
              pane.setEditorKit(kit);
              pane.setStyledDocument(doc);
           }

      }

      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Insert <img src=...> tag into HTML source and re-read.
      (Review ID: 139086)
      ======================================================================

            idk Igor Kushnirskiy (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: