Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JApplet & WindowListener

 
Matthew Orral
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic