Assume we have the following Swing application:
final JFrame frame = new JFrame();
final JPanel outer = new JPanel();
frame.add(outer);
JCompon
Have a look at the ComponentListener (or ComponentAdapter)
http://java.sun.com/docs/books/tutorial/uiswing/events/componentlistener.html
http://docs.oracle.com/javase/8/docs/api/java/awt/event/ComponentListener.html
And specifically the method:
void componentHidden(ComponentEvent e)
Invoked when the component has been made invisible.
A complete solution would look something like:
inner.addComponentListener(new ComponentAdapter() {
public void componentHidden(ComponentEvent ce) {
System.out.println("Component hidden!");
}
});
If the actions that should be carried out upon hiding is tightly coupled with the SomeSpecialCompnent, I would suggest to let SomeSpecialComponent implement ComponentListener, and add itself as a listener for the ComponentEvents in its constructor.
Another useful way (more related to add/remove operations and perhaps not suitable for your specific scenario) is to override addNotify()
and removeNotify()
.