AncestorListener example with timer

In this example we are going to see how AncestorListener can be paired with a timer in Java. This is very useful when you add or remove components to your Java Application and you want to monitor the relationship changes that follow these actions.

Basically, all you have to do in order to work with AncestorListener with a timer in Java is:

  • Create a new AncestorListener instance.
  • Override the methods that correspond to the events that you want to monitor about the ancestor changes e.g, ancestorAdded, ancestorMoved, ancestorRemoved and customize as you wish the handling of the respective events. Now every time one of these events occurs, the corresponding method will be executed.
  • Use addAncestorListener to add the AncestorListener to a specific component. Now when you add a this component to another one the event will be handled with the execution of ancestorAdded method.
  • Use a TimerTask and a Timer to schedule the display of the components.

Let’s take a look a the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

public class Ancestor {

    public static void main(String args[]) {

  final JFrame jFrame = new JFrame();

  Container cPane = jFrame.getContentPane();

  JButton jButton = new JButton("Hide for 5 seconds!");

  ActionListener actListener = new ActionListener() {


public void actionPerformed(ActionEvent event) {


    TimerTask schedule = new TimerTask() {


  public void run() {




    Timer timer = new Timer();

    timer.schedule(schedule, 5000);




  AncestorListener ancestorListener = new AncestorListener() {


public void ancestorAdded(AncestorEvent event) {





public void ancestorMoved(AncestorEvent event) {





public void ancestorRemoved(AncestorEvent event ){




private void dumpInfo(AncestorEvent event) {

    System.out.println("   Ancestor: " + name(event.getAncestor()));

    System.out.println("   AncestorParent: "

+ name(event.getAncestorParent()));

    System.out.println("   Component: " + name(event.getComponent()));


private String name(Container container) {

    return (container == null) ? null : container.getName();




  cPane.add(jButton, BorderLayout.NORTH);

  jFrame.setSize(500, 400);


This was an example on how to work with AncestorListner in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button