JEditorPane полностью не отрисовывает картинки

Рейтинг: 1Ответов: 0Опубликовано: 29.11.2014

Приветствую, почтенные!

JEditorPane используется для вывода html с картинками. В java 8,7 часто, в 6 реже не дорисовывает картинки даже в полноэкранном режиме. revalidate(), repaint() не помогают.

Проявляется на примере работы простейшего браузера типа http://www.java-tips.org/java-se-tips/javax.swing/how-to-create-a-simple-browser-in-swing-18.html . Скриншот глюка

Нашел средство. Эта часть помогла

I came across this thread when I was searching for solutions to problems with the asynchronous loading of HTML pages by the JEditorPane.setPage method (JTextPane is essentially the same so far these problems are concerned). According to the API, you should be able to use a property-change listener to be informed of when loading of an HTML page is complete. However, when we tried this, it was clear that the listener was being called before all the images in the page had been loaded.

It is possible to workaround this bug by forcing synchronous loading of the images. The following solutions are probably too late for PaulCharo, but may be of use to other people.

To force synchronous loading of images you need to override methods in the HTMLEditor.HTMLFactory and HTMLEditorKit classes:

static class MyViewFactory
extends HTMLEditorKit.HTMLFactory
{
    @Override
    public View create (Element elem)
    {
        View view = super.create (elem);
        if (view instanceof ImageView)
                ((ImageView) view).setLoadsSynchronously (true);
        return view;
    }
}

static class MyHTMLEditorKit
extends HTMLEditorKit
{
    @Override
    public ViewFactory getViewFactory ()
    {
        return new MyViewFactory ();
    }
}

editorPane.setEditorKit (new MyHTMLEditorKit ());

This should ensure that all images have been loaded by the time that the "page" property-change listener is called (see [JEditorPane.setPage API|http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html#setPage(java.net.URL)]), but there still seems to be something queued on the event dispatch queue that you need to wait for. This means that you have to use a property-change listener that looks something like: This should ensure that all images have been loaded by the time that the "page" property-change listener is called (see [JEditorPane.setPage API|http://java.sun.com/javase/6/docs/api/javax/swing/JEditorPane.html#setPage(java.net.URL)]), but there still seems to be something queued on the event dispatch queue that you need to wait for. This means that you have to use a property-change listener that looks something like:

editorPane.addPropertyChangeListener (
    new PropertyChangeListener ()
    {
        public void propertyChange (PropertyChangeEvent e)
        {
            if (e.getPropertyName ().equals ("page"))
            {
                EventQueue.invokeLater (
                   new Runnable ()
                   {`

                       public void run ()
                       {
                            // The page, including images, should now be loaded!
                           .....
                       }
                    });
            }
        }
});

Ответы

Ответов пока нет.