user3182811
user3182811

Reputation: 115

How to stop applet in Browser using Button that is built inside applet?

I have written some line of java codes in applet and there is a button called QUIT , it's work is to terminate the execution but when applet runs in browser than it doesn't?

I have tried:

None of them work in browser what's the logic behind this?

Upvotes: 3

Views: 1928

Answers (2)

Andrew Thompson
Andrew Thompson

Reputation: 168825

  • System.exit(1);

That is for abnormal termination of an app. It should not be used here, and not used in an application unless there is a fatal error from which it cannot recover.

  • System.exit(0);

An applet might share a Java Virtual Machine with other applets. If the applet in the JVM can be seen as a guest in a guesthouse, that is like one of the guests burning down the guesthouse! It is not permitted even in an applet that is trusted.

  • Applet destroy();

That method is called automatically by the JVM when the JVM thinks it is appropriate to do so. An applet might override the method, but should not explicitly call it.

  • Applet dispose();

Same deal as destroy(), leave it to the JVM.


The easiest way to end and applet is by using AppletContext.showDocument(URL). It might work something like this (where this represents an Applet):

this.getAppletContext().showDocument(thanksForUsingOurAppletURL);

That will of course, redirect to the URL. The JVM will call the dispose() and destroy() methods. Then when it decides it appropriate to do so, (which might be '30 seconds or so' after the last applet ends), it will shut itself down.

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201447

You can invoke JavaScript from the applet to rewrite the div containing the applet. That will do it! Using JSObject, so I might use eval() with something like this

JSObject win = JSObject.getWindow(this);
win.eval("document.getElementById('#applet').innerHTML=''", null);

You might also use getAppletContext().showDocument(goodbyeUrl)

Upvotes: 0

Related Questions