Java tutorial
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; public class AncestorSampler { public static void main(String args[]) { JFrame frame = new JFrame("Ancestor Sampler"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); AncestorListener ancestorListener = new AncestorListener() { public void ancestorAdded(AncestorEvent ancestorEvent) { System.out.println("Added"); } public void ancestorMoved(AncestorEvent ancestorEvent) { System.out.println("Moved"); } public void ancestorRemoved(AncestorEvent ancestorEvent) { System.out.println("Removed"); } }; JButton bn = new JButton(); bn.addAncestorListener(ancestorListener); frame.add(bn); //frame.remove(bn); frame.setSize(300, 200); frame.setVisible(true); } }