1

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 JEditorPanes 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();
      }
    });
stemm
  • 5,960
  • 2
  • 34
  • 64

2 Answers2

2

consider to use SwingWorker, with example or there is possible start BackGround Task from Runnable#Thread (output must be wrapped into invokeLater()

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Thanks. But I use them. The trouble is - if some image url is unavailable - all of JEditorPanes can't download their pictures. Why? – stemm Aug 05 '11 at 15:20
  • @stemm really you have to post runnable code that shows your issee(e), including code where is validated/tested URL..., because SWingWorker is pretty cancelable – mKorbel Aug 05 '11 at 15:25
  • do you create a new Object again and again ..., re-use JComponents why not use JLabel http://download.oracle.com/javase/tutorial/uiswing/components/label.html for Image/ImageIcon http://download.oracle.com/javase/tutorial/uiswing/components/icon.html, why this very complicated way, BTW for correct repaint is sometimes required revalidate() + repaint() – mKorbel Aug 05 '11 at 16:21
  • Thanks. In fact, I've describet only small part of my code. Of course there is not only pictures in JEditorPane, there is also enough other html elements. And I use repaint too, but as for me it is not very principial in described situation. So to sum up: I don't have deadlocks, I'm loading html from web, creating JEditorPane, and only after calling all constructors, I put JEditorPane into GUI. But, JEditorPane tries to download images in its own thread(!?) and it hungs. So is there any ability to interrupt that thread? – stemm Aug 05 '11 at 18:50
  • Sorry, I don't understand. Hmm... OK, could you explain how does JEditorPane downloads images from url? (in tags ). Does JEditorPane downloads them asynchronously? (because first of all - appears text, and images appears later) – stemm Aug 05 '11 at 20:42
  • what's type of Thread ("`So is there any ability to interrupt that thread?`") – mKorbel Aug 05 '11 at 20:48
  • @stemm `Does JEditorPane downloads them asynchronously?` not exactly but SwingWorker can do that, as suggested (@Hovercraft Full Of Eels) look here for multithreading with SwingWorker http://stackoverflow.com/questions/6171414/how-to-share-data-with-two2-swingworker-class-in-java/6186188#6186188 – mKorbel Aug 05 '11 at 20:53
  • I've just interested about internal mechanism of downloading images in JEditorPane. Not about making multithreading (I do that). But I interested - if JEditorPane in process of parsing html finds url of image - does JEditorPane creates own thread for picture downloading and continues parsing? Or JEditorPane pauses parsing and starts downloading image? – stemm Aug 05 '11 at 21:01
1

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You're making swing calls from within the background thread which will mess you up. Please read: [Concurrency in Swing](http://download.oracle.com/javase/tutorial/uiswing/concurrency/) – Hovercraft Full Of Eels Aug 05 '11 at 16:37