- Have you class implement the
windowClosing
event of theWindowListener
. - Add an
AncestorListener
to your custom component and listen for theancestorAdded
event. This event is generated when you add your component to a visible GUI or when the GUI containeing your comonent is realized. - In the
ancestorAdded
event you add your WindowListener to the frame. You can get the current frame by using theSwingUtilties.windowForComponent(...)
method.
Now all the logic is self contained in your class.