Is there a way to reload/refresh a java applet from within the applet itself?

Deadly 提交于 2019-12-05 08:25:03

问题


I have a button on my applet (contained in a browser) that I would like to make reload or refresh the entire applet one of two ways:

  1. Refresh the applet itself without having to refresh the browser
  2. Refresh the entire browser

Is this possible from within the applet?


回答1:


Use the AppletContext.showDocument(...) method:

applet.getAppletContext().showDocument(applet.getDocumentBase(), "_self")

That will load the document containing the applet in the same window/frame the applet is already loaded in.




回答2:


For the first part of your question, there are a few approaches. You could make a call to JavaScript and remove/re-add the applet from there. Use LiveConnect to call from Java to JS, and be sure to use the "MAYSCRIPT" attribute in your applet tag.

In your JavaScript method, remove the applet from the DOM and add a fresh one in its place.

You might also put the applet in an iFrame, which gives you more options.

But what is your aim in reloading the applet? It would be a safer approach to solve this problem completely within the applet instead of relying on JavaScript (and your JAR will likely be cached anyway, so you're basically just restarting the same applet).

I suggest either:

  • code your applet so that you can reset its GUI and data with a method call, or
  • run the applet via a small wrapper applet which can drop/recreate the real applet it is displaying.

For the wrapper applet approach: implement AppletStub, instantiate the real applet, and display it as the center of a BorderLayout, something like this (in your start() method):

Applet applet = new TheRealApplet();
applet.setStub(this);
this.setLayout( new BorderLayout() );
add( applet, BorderLayout.CENTER );
applet.init();
applet.start();

...then call applet.stop() in the wrapper's stop() method, and add a new method that restarts the applet -- by stopping it, removing it from the layout, and adding a fresh instance.



来源:https://stackoverflow.com/questions/1046691/is-there-a-way-to-reload-refresh-a-java-applet-from-within-the-applet-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!