我遇到以下问题,即我将JTextPane与HTMLEditorKit结合使用,并向窗格动态添加内容。内容可以跨越几行,还包含很多图像(小图标)。现在的问题是,如果我通过以下方式插入一堆带有图标的文本:

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


结果是html代码中的图像需要很长时间才能显示在屏幕上(〜1秒):

java - 使用HTMLEditorKit缓慢解析动态JTextPane-LMLPHP

问题:有没有办法通过以下方法来缓冲窗格中显示的图像(如果是html代码):

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


由于HTMLEditor,我无法使用pane.insertIcon。是否可能需要某种容器将其设置为不可见,将内容添加到窗格中,然后再将容器设置为可见?

最佳答案

@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);
}


完美无缺,现在可以正常工作。我还对HTMLEditorKit的ImageView进行了更改:

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;
            }
        };
    }
}

09-27 14:01