File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Applets and the fly likes JApplet & WindowListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "JApplet & WindowListener" Watch "JApplet & WindowListener" New topic

JApplet & WindowListener

Matthew Orral

Joined: Jul 27, 2003
Posts: 11
I need to call a method only after the JApplet is closed, i was thinking about windowlisteners but in the JApplet init method, adding ((JFrame) this.getParent()).addWindowListener(this) didn't work (compiles well,but raises exceptions) (the JApplet implements windowListener methods, for the moment all blanks).
How can I add a window listener to a JApplet (not a standalone app with a JFrame, but a simple applet for the web (whitout main))?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The code throws an exception because the parent of a JApplet won't usually be a JFrame (it'll pretty much never be -- it might be the content pane of a JFrame, but not the JFrame itself.) But most of the time, it won't be a Java window at all -- it's the browser window.
You don't want to use a Window listener. All you need to do is to override either the stop() method of JApplet (which is called when the user browses away from the page the applet it on; start() may be called again if the user browses back) or destroy() (which should be called at the very end of the applet's life.)

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: JApplet & WindowListener
It's not a secret anymore!