开发者

Detect when an Applet is loaded

How can I detect whether a java-applet has been loaded successfully without polling the browser /using setTimout ?

Is there any event that I can bind to?

The problem is: there is a pop up asking the user whether he trusts the applet or not, and that takes a few seconds until the user clicks "Yes" ,meanwhile my 开发者_C百科code fails to execute because the applet has not been loaded.


You mean from javascript ? You can ask the applet. Have it set a boolean to true in init(), and return this in a method you call ... oh wait, you wrote "without using setTimeout". Well, in this case, I guess you have to go the other way around, and have your applet call a javascript method (for instance with JSObject.getWindow(this);)... But I think this is more difficult/bug prone than using a timer until the applet is loaded.


I know this question is quite old, but my preferred way to do this, because Java Loading Screens are pretty tough to implement, and setting up a full-screen div with spin.js is not, is to tell the applet to call a JavaScript function which hides the loading div when the init() function of the java applet runs.

Calling a JavaScript function from Java is pretty simple:

import java.net.*;

public void init() {
    if (!isJava5OrSuperior()) {
      showError(getLocalizedString("requirementsMessage"));
    } else {
      createAppletApplication();
      try {
        getAppletContext().showDocument
          (new URL("javascript:appletReady()"));
        }
      catch (MalformedURLException me) { }
    }
  }

This will call the appletReady() function of your page, whether it's linked from another JS file or scripted directly in the HTML, as soon as the Applet itself runs. So instead of polling your applet indefinitely, you just run the function when its ready.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜