Link to home
Start Free TrialLog in
Avatar of Naeemg
Naeemg

asked on

JEditorpane (opening webpage keeps stucked all GUI untill all page loaded)

hi everyone,
i've a riched GUI application, and in that i've used JEditorpane to open webpages in it.
i've been experiencing many times, that whenever a website is opened in JEDitorpane all my application gui get stucked, it keeps stucked untill all webpage with its all images or contents not loaded.

and all GUI becomes white, that it seems the application is hanged, whereas JEditorpane keeps downloading page contents at the backend but user can't view its progress.

there is also no way to get progress of loading the all webpage as in and web browser.
can any one plz help me that website loads smoothly and don't stuck other gui components, so that user can work on other components while the website is being loaded in JEDitorpane??

for now i only need that my GUI don't go stuck by JEditorpane and webpages should load smoothly as in Internet Explorer.

plz help me, i need solution urgent.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Best in this case to use a SwingWorker

https://swingworker.dev.java.net/
Avatar of Naeemg
Naeemg

ASKER

each time i load new url in JEditorpane i use a class which implements Runnable. also before loading i execute a timer thread which periodically repaint other GUI, but it fails in this case.
It's not working then as your GUI would not get stuck. The SwingWorker would be much easier
Avatar of Naeemg

ASKER

ok for u i test swing worker, but guide me how to use it, iv'e download its swing-worker.jar file.
remember pal i've less pionts in my account :( and i want working solution , so plz guide me to right path.
thanks
There are examples there. Load the url in construct and set the text in finished
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Naeemg

ASKER

can somebody demonstrate , how to use swingworker class , i've not gotton any example after searching in the web for 1 day.
Avatar of Naeemg

ASKER

i've tested swingworker several times with my application, but no success. JEditorpane behaviour didn't change.
I've realized that, it stucks mostly when page contains images.
If JEditorpane only get stuck, it was not big problem, but it should not stuck all other components withing the same application.
can anyone plz help me , its too urgent
plz solve my this problem. i've not got any solution anywhere about that, and i've less time.
i've also posted it on sun developer site, but nobody could answer it.
>>it was not big problem, but it should not stuck all other components withing the same application.

It shouldn't do. Perhaps you can post what you did with the SwingWorker?
Avatar of Naeemg

ASKER

     SwingWorker worker = new SwingWorker() {
        public Object construct() {
          return  startDownloadingURL(); // my this method loads this given URL in jeditorpane
        }
        public void finished() {
          jEditorPaneWebBrowser.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
          frame.jLabelStatusBarInfo.setText("Done.");
        }
      };


// the above code executed in a Runnable thread method run().
i've tested it without it also, but same problem.
But you're not setting the text itself ...
.. or are setting it in the wrong place. It should be set in finished
Avatar of Naeemg

ASKER

you mean i should load url or text in jeditorpan in finish method?

but as of examples about swingworker i done that.
You should set the text in finished. You read the url in construct
Avatar of Naeemg

ASKER

i couldn't understand , would u please explain.

i only open url to jeditorpane not setting text in it.
and in finish method i only set text to a JLable to indicate in status bar that work is done.

do u want me to load url method in finish method? then what to load in consturctor method?
>>i only open url to jeditorpane not setting text in it.

Can you tell me what the difference is? Whatever is in the url, you are setting as the editor pane's text presumably?
Avatar of Naeemg

ASKER

here is the full code, check it

  public class LoadURLinJEditorpane implements Runnable
  {
    String url;
    Thread thread;

    /**
     * Constructor
     * @param u String
     */
    public LoadURLinJEditorpane(String u)
    {
      this.url = u;
    }

    public void run()
    {
      SwingWorker worker = new SwingWorker() {
        public Object construct() {
          return  startDownloadingURL();
        }
        public void finished() {
          jEditorPaneWebBrowser.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
          frame.jLabelStatusBarInfo.setText("Done.");
        }
      };
      worker.start();
    }

    Object startDownloadingURL()
    {
      try
      {
        jEditorPaneWebBrowser.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        jEditorPaneWebBrowser.setContentType("text/html");
        //jEditorPaneWebBrowser.setEditorKit(new WebBrowserHTMLEditorKit());
        URL u = new URL(url);
      if(u != null)
      {
           jEditorPaneWebBrowser.setCaretPosition(0);
           jEditorPaneWebBrowser.setPage(u);
        }
      }
      catch(Exception ex)
      {
        frame.jLabelStatusBarInfo.setText("Done.");
        jEditorPaneWebBrowser.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        return "Error";
      }
      return "All Done";
    }

    public void start()
    {
      try
      {
        thread = new Thread(this);
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.setName("LoadURLinJEditorpane");
        thread.start();
      }
      catch(Exception ex)
      {
        jEditorPaneWebBrowser.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        frame.jLabelStatusBarInfo.setText("Done.");
      }

    }

    /**
     * Stops this thread
     */
    public synchronized void stop()
    {
      if(thread != null)
      {
        thread.interrupt();
      }
      thread = null;
      notifyAll();
    }
  }


 public class WebBrowserHTMLEditorKit extends HTMLEditorKit{

         public Document createDefaultDocument() {
                 Document document = super.createDefaultDocument();
                 if(document instanceof AbstractDocument){
                         ((AbstractDocument)document).setAsynchronousLoadPriority(-1);
                 }
                 return document;
         }

       }

Avatar of Naeemg

ASKER

i've noticed that, this GUI stuck problem mostly happens when the page being loaded in JEditorpane has too many Images/Grahpics.
The way i'd do it is just read it into a String in construct and call setText in finished
Avatar of Naeemg

ASKER

sorry, wat text ?
you mean first open url and get its inputStream in String, then set that String to JEditorpane as text?
Correct
Avatar of Naeemg

ASKER

but, i've noticed that, this GUI stuck problem mostly happens when the page being loaded in JEditorpane has too many Images/Grahpics.

it means, if we get inputStream, it will only give us HTML data, later when we set it to JEditorpane.
then if there are some <img > tags in HTML,
then JEditorpane will try to invoke these images, and download it. and again application will stuck.
what do u think?