2017-03-23 62 views
1

我遇到以下问题,我正在将JTextPane与HTMLEditorKit一起使用,并向窗格中动态添加内容。内容可以在多行,也包含大量图像(小图标)。现在的问题是,如果我插入例如一堆带图标的文字,通过:使用HTMLEditorKit缓慢解析动态JTextPane

editorKit.insertHTML(doc, doc.getLength(), htmlCode, 0, 0, null); 

结果是,花费很长的时间在HTML代码中的图像出现在屏幕上(〜1秒) :

enter image description here

问:有没有一种方法,来缓冲在窗格中显示的图片,如果它的HTML代码通过:

imgsrc = "file:/" + imgSRCOnHDD; 
imgsrc = imgsrc.replace("\\", "/"); 
imgSub = "<img height=\"18\" width=\"18\" style=\"vertical-align:middle;\" src='" + imgsrc + "'></img>"; 

我不能使用pane.insertIcon b由于HTMLEditor。是否有可能将某种容器设置为不可见,将内容添加到窗格,然后将容器设置为可见?

+0

也许'的SwingWorker '? [Profile](http://stackoverflow.com/q/2064427/230513)可以肯定。 – trashgod

+1

http://java-sl.com/tip_local_images.html你可以尝试在自己的缓存立体图像 – StanislavL

+0

非常感谢你的提示,解决了问题 –

回答

1

评论者@StanislavL导致我解决了这个问题,尽管他没有提供的链接,但没有工作,我无法找出原因,这些URL是正确的,我仔细检查过,但设置缓存后,窗格总是会为每个图像显示“断链”图片。所以,我发现这个职位,有一个HTMLEditorKit的缓存机制的认识:

https://stackoverflow.com/a/27669916/7377320

所提到的代码:

public class ImageCache extends Hashtable { 

    public Object get(Object key) { 

     Object result = super.get(key); 

     if (result == null){ 

      result = Toolkit.getDefaultToolkit().createImage((URL) key); 
      put(key, result); 
     } 

     return result; 
    } 
} 

...

Dictionary cache = (Dictionary) pane.getDocument().getProperty("imageCache"); 

if (cache == null) { 

    cache = new ImageCache(); 
    pane.getDocument().putProperty("imageCache", cache); 
} 

曾为完美无瑕,并它正在工作。我还做了这种变化的ImageView的一个HTMLEditorKit:

public class MyImageView extends ImageView { 

    public MyImageView(Element elem) { 
     super(elem); 
    } 

    @Override 
    public Icon getNoImageIcon() { 
     return null; 
    } 

    @Override 
    public Icon getLoadingImageIcon() { 
     return null; 
    } 
} 

这不会在窗格中显示任何损坏的链接的图片了,还没有“预加载”断开链接的图片了。

一个HTMLEditorKit:

public class MyHTMLEditorKit extends HTMLEditorKit { 

    @Override 
    public ViewFactory getViewFactory() { 

     return new HTMLEditorKit.HTMLFactory() { 

      public View create(Element e) { 

       View v = super.create(e); 

       Object o = e.getAttributes().getAttribute(StyleConstants.NameAttribute); 

       if (o instanceof HTML.Tag) { 

        HTML.Tag kind = (HTML.Tag) o; 

        if (kind == HTML.Tag.IMG) { 
         return new MyImageView(e); 
        } 
       } 

       if (v instanceof InlineView) { 

        return new InlineView(e) { 

         public int getBreakWeight(int axis, float pos, float len) { 
          return View.GoodBreakWeight; 
         } 

         public View breakView(int axis, int p0, float pos, float len) { 

          if (axis == View.X_AXIS) { 

           checkPainter(); 

           int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len); 
           if (p0 == getStartOffset() && p1 == getEndOffset()) { 
            return this; 
           } 

           return createFragment(p0, p1); 
          } 

          return this; 
         } 
        }; 
       } else if (v instanceof ParagraphView) { 

        return new ParagraphView(e) { 

         protected javax.swing.SizeRequirements calculateMinorAxisRequirements(int axis, javax.swing.SizeRequirements r) { 

          if (r == null) { 
           r = new javax.swing.SizeRequirements(); 
          } 

          float pref = layoutPool.getPreferredSpan(axis); 
          float min = layoutPool.getMinimumSpan(axis); 

          // Don't include insets, Box.getXXXSpan will include them. 
          r.minimum = (int) min; 
          r.preferred = Math.max(r.minimum, (int) pref); 
          r.maximum = Integer.MAX_VALUE; 
          r.alignment = 0.5f; 

          return r; 
         } 

        }; 
       } 

       return v; 
      } 
     }; 
    } 
}