Reputation: 1078
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.
Upvotes: 1
Views: 426
Reputation: 4726
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.
Upvotes: 0
Reputation: 51559
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.
Upvotes: 0