Question

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!

Was it helpful?

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

OTHER TIPS

I would write something like that

class ListenToWindow
extends WindowAdapter
{
MyInternalFrame frame;

public void windowClosed(event)
   {
   this.frame.removeAllTheRequiredListeners();
   }
}

(...)
JFrame window;
MyInternalFrame frame;
(...)
window.addWindowLister(new ListenToWindow(frame));
(...)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top