JEditorPane problem with asynchronous image downloading
I'm trying to create simple Swing application, which contains few JEditorPanes inside. Each JEditorPane contains text with html tags inside. And also some panes contains html with tags <img src='http://some.url' />
, it means that images might be somewhere in web. And the problem is - if one of the image urls is unavailable - all of my JEditorPanes and whole application hungs. (I'm constructing JEditor开发者_如何学CPanes in own thread, and after constructing put them into main frame using SwingUtilities.invokeLater(...)
)
I believe that images downloading into JEditorPanes asynchronously, is there any ability to kill these hunging image downloading threads?
Or maybe, there is better solution?
Thanks
P.S. SwingWorker is used. The trouble is - if some image url is unavailable - all of JEditorPanes can't download their pictures. In fact they doesn't hung, but the can't download images. Why?
P.P.S.
Background thread:
JEditorPane jtp=new JEditorPane();
jtp.setContentType("text/html");
jtp.setPreferredSize(newDimension(20,250));
StringBuilder sb=new StringBuilder();
sb.append("<img src='").append(url).append("'/>");
jtp.setText(sb.toString());
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
myPanel.add(rigid,0);
myPanel.add(jtp,0);
myPanel.revalidate();
}
});
consider to use SwingWorker, with example or there is possible start BackGround Task from Runnable#Thread (output must be wrapped into invokeLater()
I agree with mKorbel that a background thread is the way to go (1+ to his answer), and that SwingWorker is one way to do this. If you need to have multiple background threads running concurrently, be careful when using SwingWorker though since there was recently a bug that caused all SwingWorkers to use only one thread. In that case consider using Executors/Futures.
精彩评论