We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


How to reload applet from inside the applet?

msmolyak asked
Medium Priority
Last Modified: 2008-03-06
My applet has trouble executing under Netscape browser. The browser starts executing the init() method and hangs somewhere before finishing it (no error messages or exceptions). As a result the applet's GUI does not get displayed. I was trying to find the reason why the applet hangs but this question is about a different issue.

I noticed that if I reload the hung applet using browser's Reload button (or simply switch to a different page and then come back to the original applet) the applet executes normally. (Browser starts executing init() again and this time finishes it).

My question is whether it is possible to simulate this behavior from within an applet. I cannot use getAppletContext().showDocumnent() since the HTML file for this applet gets build dynamically and does not exist on disk.

What I did was to create a new class RestartApplet() which extended Thread. At the beginning of init() method I created new instance of that class passing it an applet as an argument and call its start() method (start a thread). At the end of init() that thread gets killed.

That thread sleeps for N seconds and then (if its still alive which means init() never finished) tries to restart an applet. I do it by executing the following code:


That works fine in AppletViewer but does not quite work in Communicator.

Question is: what is the appropraite way to reload an applet from inside (so that the browser new that it is reloaded) given the restrictions described above.

Thank you.
Watch Question


You definitely on the right track.  Most likely
the problem you are having comes from Communicator
still being so buggy.  I would suggest calling
repaint() and possibly creating a new method in
the applet that you call from the thread that
checks things out and updates any necessary values.




Thanks for a quick answer. I did try calling repaint, it does not change anything. My cocern is that when I reload the frame where applet is displayed, it reloads and displays OK. But when I execute the code above the following thing happens: the init() method gets executed, the start() method gets executed (that is, applet does not hang this time) and then small part of the applet GUI appears on the screen. (That is, most of the frame is grey an only a small part has what applet is supposed to display). It is not even a component, just part of the GUI. Thus it looks like display problem and it is probably caused by the way I reload the applet.

Any more insights?



Yeah, you're definitely encountering Netscape problems.  
What are the components that aren't showing up?  Are they
native components or your own?  



The components are from a third parties. Again it is possible that there is something wrong with the browser or with the components (probably the browser) but that's not the ponit. The point is that reloading code in the frame through the browser (by hitting Reload button or executing Frame Reload from the right mouse button menu) works fine, while my code does not. I am trying to mimic in my code what the browser does when reloading the applet.



Sorry, but I have to reject an answer since I did not get the problem I was experiencing resolved. In the meantime I found another way how to accomplish reloading using JavaScript. Thanks for your help anyway.

You can easily emulate that senario by:

1) define a boolean flag in the applet, specifying if it was just loaded (i.e. in the "init" method set it to true).

2) In the "start" method check for the value of the variable. if it is true then:
   (a) Set it to false.
   (b) Tell the browser (using the AppletContext) to go to a
       known location (known page). That page should have just
       a javascript function which accesses the browser history
       and tells it to go back one.



Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts



I found what I think a simpler way to accomplish this using JavaScript. It requires no changes to Java code. It looks like the following:

setTimeout("reloadFrame()", 13000);
function reloadFrame()
      alert("Applet is active: " + document.dbTree.isActive());
      if (!document.dbTree.isActive())
What it does is waits for N second (13 in this case) and then checks whether the applet is active. If it is not, it reloads the page.

I will give you the points if you can point out how to get rid of the non-deterministic element (wait time). That is how to reload the applet only if it's not loaded without having to guess how much time it may take to load (that is 13 sec mya be too little if the applet is about to load properly or it's too much if the applet is not going to load the first time). Note that the applet loads fine on some browsers and does not load the first time on others.
Do you have an indication when its not loaded properly ? is the "start" method called ?


As I mentioned in the initial description, when the applet does not load properly it hangs somewhere in the init() method. If the applet hangs and I execute Reload Frame command, it loads properly (executing init() and start() methods). After start method gets executed the isActive() method returns true. That's what I am using in my solution.
Well I don't have the implementation answer to your solution.
However, Consider using a thread (class which implements the Runnable interface, and implements a "run" method to do the follwing) which will execute (first thing in the "init" method) and check every several (milli)seconds if the applet is active and if after X attempts the applet is inactive, it can tell the browser to go to a special page which will contain a JavaScript->Go-Back-In-History code.



The poblem still remains: how many attempts do you consider sufficient to declare applet dead? It is not much different from waiting for N seconds in JavaScript code is it?
That depends on your "init" code. How much code have you put in it ?

I'd say 5 seconds for an init method is .... ;-)

Run some tests (with no reloads, just report) to see how long does it take for the applet to complete the "init" method, reach the "start" method and complete the "start" method.



That's easier said than done because the system I am writing goes to dozens of customers who are going to run this particular applet on a variety of systems and browsers. That's why I would like to get rid of this timing problem altogether.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.