views:

1197

answers:

2

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  A: 

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.

liminal
A: 

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.

Rob Whelan