1

My application shows RSS entries in a JEditorPane. It works not that bad, but I've encountered a serious issue lately while displaying a gif. Here's a small test case :

public class JEditorPaneTest
{
    public static void main(String[] args)
    {
        JFrame dialog = new JFrame("JEditorPane test");
        dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container c = dialog.getContentPane();

        c.setLayout(new BorderLayout());
        JEditorPane editorPane = new JEditorPane();
        editorPane.setEditorKit(new HTMLEditorKit());
        editorPane.setText("<html><body><img src=\"http://feeds.feedburner.com/~ff/MacRumors-All?d=yIl2AUoC8zA\"></body></html>");
        c.add(new JScrollPane(editorPane), BorderLayout.CENTER);

        dialog.pack();
        dialog.setVisible(true);
    }
}

The unanimated gif is flickering and the CPU usage is up to 300% (quad-core computer) on Mac OS X or 50% on Windows 7. This issue is even more severe since after disposing the editor pane, the CPU usage is still that high.

Looking at some profiling, it looks like 50% of the cpu usage time is in the Event dispatcher thread and the other 50% is in sun.awt.image.ImageFetcher.run().

Another interesting fact is that it happens also when embedding html in a JLabel.

(EDIT : 9 march 2012) Another interesting fact is that if the file is downloaded locally first (and accessed by file://...) or even, being a resource do not fix anything. But if the image is shown in ImageIcon in a JLabel, there is no flickering and high CPU usage. Somehow, I really need to use a HTML document in a JEditorPane to render some basic HTML formatting.

I am looking for a general solution to avoid censoring images from feedburner.com and, these gifs from feedburner.com are the only one I found yet, but I would like prevent having the bug with all other images that could behave the same way.

Thanks a lot.

Béatrice Cassistat
  • 1,048
  • 12
  • 37
  • 2
    My guess is that you would be better off reading the image into your code using the HttpURLConnection class, then displaying the image in your editorPane. – Gilbert Le Blanc Mar 02 '12 at 17:46
  • 1
    Thanks for your suggestion Gilbert, but actually using the local saved file and using does not fix the problem. – Béatrice Cassistat Mar 02 '12 at 18:36
  • 2
    I guess I wasn't clear. Read the image into your Java program, getting a BufferedImage or Image as a result. Then convert to an ImageIcon for display in your editorPane. – Gilbert Le Blanc Mar 02 '12 at 18:39
  • Interesting. Are you suggesting I should override HTMLEditorKit to do so? I can't find any method in JEditorPane to inject images directly into the document. – Béatrice Cassistat Mar 02 '12 at 21:08
  • I had a JLabel on my mind. See if my answer makes more sense. – Gilbert Le Blanc Mar 03 '12 at 03:59
  • It would work. But I need richtext too in the document. Thanks for your suggestion. – Béatrice Cassistat Mar 04 '12 at 19:53
  • Those suggestions are all meaningless. The problem is with JEditorPane, Ive noticed this in a program of mine too, writing a IRC client, which shows animated gifs. JEditorPane has problems with just showing a few gifs on the screen, regardless where they come from, just a few gifs will let the CPU usage explode. Ive not found a workaround for this so far. If you google JDK-4821632 youll find some other reports claimed to be fixed, but it is not. Still present today. Also it seems to be rendered even if scrolled out of the screen blowing up cpu usage. – M. H. Apr 11 '22 at 08:42

2 Answers2

0

I know that this is very old, but I was also having some issues with High CPU usage in JEditor pane in a different project so I decided to try out your test case. By adding debugging to the event queue I determined that it was continually repainting the screen because that particular image is an animated gif. It may look unanimated, but it is, in fact, an animated gif.

If you convert it to an unanimated gif or PNG, it will resolve the issue.

steve hannah
  • 4,586
  • 11
  • 18
0

Get the image from the website using HttpURLConnection.

Save the image to your local drive as a gif file.

Try this in your editorPane.setText method:

<html>
<body>
<img src="images/picture.gif">
</body>
</html>

Where the image source points to the gif file you saved.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111