Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Detect when a JPanel dies  RSS feed

 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a custom JPanel that is part of a JFrame. When the JPanel is constructed is make some custom calls to register things. When the JFrame that added this JPanel closes I want the JPanel to detect this so that it can unregister things. I do not want the JFrame to have to tell the JPanel that it is closing if I don't have to. I.E. the JPanel registered things in its constructor I want it to clean up after itself when it closes (I don't want to have to reply on the JFrame to tell it to clean it self up with a method call.

Is there a listener I can use in the JPanel?

Thanks!
 
Francois Nadeau
Ranch Hand
Posts: 46
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Billy,

Off hand I do not know of a listener for what you want, but since you have extended JPanel you can override the removeNotify function by adding the following code to your custom JPanel.



According to the Java docs, this functions is called to "Notifies this component that it no longer has a parent component", so it should work for what you want to do.

Cheers.
 
Bobby Anderson
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perfect, that worked great!

As aside I also added an override to addNotify to be consistent and removed "registering things" from the constructor.



Thanks for the help!

Billy
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!