Listening to JComponent Events with an AncestorListener : AncestorListener « Swing Event « Java Tutorial






Listening to JComponent Events with an AncestorListener
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);
  }
}








15.7.AncestorListener
15.7.1.Listening to JComponent Events with an AncestorListenerListening to JComponent Events with an AncestorListener