Solved

How to reload applet from inside the applet?

Posted on 1997-10-06
13
511 Views
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:

        applet.stop();
        applet.destroy();
        applet.init();
        applet.start();
        applet.validate();
        applet.show();

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.
0
Comment
Question by:msmolyak
  • 7
  • 4
  • 2
13 Comments
 
LVL 4

Expert Comment

by:rembo
ID: 1227972


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.

-Tony


0
 
LVL 5

Author Comment

by:msmolyak
ID: 1227973
Tony,

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?

Michael
0
 
LVL 4

Expert Comment

by:rembo
ID: 1227974


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

-Tony


0
 
LVL 5

Author Comment

by:msmolyak
ID: 1227975
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.
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1227976
Tony,

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.

Michael
0
 
LVL 3

Accepted Solution

by:
EricKaplan earned 100 total points
ID: 1227977
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.

Luck,

-Eric.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Author Comment

by:msmolyak
ID: 1227978
Eric,

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:

<SCRIPT LANGUAGE="JavaScript">
setTimeout("reloadFrame()", 13000);
function reloadFrame()
{
      alert("Applet is active: " + document.dbTree.isActive());
      if (!document.dbTree.isActive())
      {
            location.reload()
      }
}
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.
0
 
LVL 3

Expert Comment

by:EricKaplan
ID: 1227979
Do you have an indication when its not loaded properly ? is the "start" method called ?
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1227980
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.
0
 
LVL 3

Expert Comment

by:EricKaplan
ID: 1227981
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.

-Eric.
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1227982
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?
0
 
LVL 3

Expert Comment

by:EricKaplan
ID: 1227983
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.

-Eric.
0
 
LVL 5

Author Comment

by:msmolyak
ID: 1227984
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now