This week's giveaway is in the Beginning Java forum.
We're giving away four copies of Bad Programming Practices 101 (e-book) and have Karl Beecher on-line!
See this thread for details.
Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JApplet & WindowListener  RSS feed

 
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))?
 
author and iconoclast
Sheriff
Posts: 24219
40
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.)
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!