Search code examples
javaswingcomponentslisteners

How can a JComponent notified about the closed event of it's parent J(Internal)Frame?


I want to write a component that monitors other activities, but it's listeners are to be removed when the component's window is closed.

I don't want to write this removal code many times, but want the component to handle it itself. (How) can i do it?

Thanks!


Solution

  • The JFrame class (which is the window) has a processWindowEvent callback that takes a single parameter called Windowevent

    Register this callback and if the parameter is of WINDOW_CLOSED you can call the removal code inside.

    In the end the removal code is only written once (as you want it).

    See the API for more details.

    Update: See also this