Ancestor Listener is An Event Listener Interface in the Java swing library. An Ancestor Listener to the change of the component itself or the Ancestors of that component. This is a good candidate to use in the situation where you want to monitor the change of the JComponets. The change can be either making the component visible or invisible, the movement of the component, or completely disposing or removing the component.

Ancestor Listener

The Ancestor Listener Interface provides a very good Functionality for the monitoring of the Application that is out there in our Graphical User Interface. We will have a look at the Example. This Event Listener provides Interface provides us three Method to monitor three types of activities or events on our Component.  

Methods in Ancestor Listener Interface

We have three methods in the Ancestor Listener Interface. It means that the class that we want to implements Ancesotor Listener Interface should have to implement all of the three methods. These three methods are invoked with the three different activities or events that can happen to the Jcompont. We will have a look at each of them along with a meaningful full example for better understanding. 

  1. ancestorAdded(params)

  2. This method is called when the source component that is registered with the Ancestor Listener Interface is set to either visible or invisible. The return type of this method is Void and the parameter passed to this method is AncestorEvent type. It means that this method is monitoring the visibility of the component or its ancestor components. When there is a change in the visibility of any component then this method is invoked automatically, and the code in this method is executed. We will look into an example of this method we will only implement this method in our program and will see how it works. below is the code of the program and the comment will help you understand what is going on.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    package AlixaProDev.EventListeners;
    
    import com.sun.security.auth.module.JndiLoginModule;
    
    import javax.swing.*;
    import javax.swing.event.AncestorEvent;
    import javax.swing.event.AncestorListener;
    import java.awt.*;
    
    
    public class AncestorListenerDemo extends JFrame {
        Label label;
        AncestorListenerDemo(){
            label = new Label("Okay");
    
            this.getRootPane().addAncestorListener(new AncestorListener() {
                // when the label is added to the frame it is visible so this 
                // method will be called
                @Override
                public void ancestorAdded (AncestorEvent event) {
                    label.setText("i am added to the frame");
                }
    
                @Override
                public void ancestorRemoved (AncestorEvent event) {
    
                }
    
                @Override
                public void ancestorMoved (AncestorEvent event) {
    
                }
            });
    
            this.setLayout(new BorderLayout());
            this.setSize(new Dimension(400,400));
            this.add(label,BorderLayout.CENTER);
            this.setVisible(true);
        }
    
        public static void main (String[] args) {
            new AncestorListenerDemo();
        }
    
    }
    
  3. ancestorMoved(params)

  4. ancestorMoved is another method that is present there in the ancestor listener interface which is a void type method and returns void ant the parameter of this method is an AncestorEvent type. This method is invoked when a component or the ancestor of the component that is registered with the handler is moved. Look at the below code, comments on the code will help you understand.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    package AlixaProDev.EventListeners;
    
    import com.sun.security.auth.module.JndiLoginModule;
    
    import javax.swing.*;
    import javax.swing.event.AncestorEvent;
    import javax.swing.event.AncestorListener;
    import java.awt.*;
    
    
    public class AncestorListenerDemo extends JFrame {
        JLabel label = new JLabel("Move the Window");
        AncestorListenerDemo(){
            this.getRootPane().addAncestorListener(new AncestorListener() {
                // when the label is added to the frame it is visible so this
                // method will be called
                @Override
                public void ancestorAdded (AncestorEvent event) {
                }
    
                @Override
                public void ancestorRemoved (AncestorEvent event) {
                }
    
                @Override
                public void ancestorMoved (AncestorEvent event) {
                    label.setText("Window is moved brother");
                }
            });
    
            this.setLayout(new BorderLayout());
            this.setSize(new Dimension(400,400));
            this.add(label,BorderLayout.CENTER);
            this.setVisible(true);
        }
    
        public static void main (String[] args) {
            new AncestorListenerDemo();
        }
    
    }
    
  5. ancestorRemoved(params)

  6. The final method that should be implemented by the class that wants to use the AncestorListener Interface is the ancestorRemoved method. The return type of this method is void and like the other two methods, this method is also the parameter type AncestorEvent. This method is invoked when a component by itself or any ancestor component of the source component is removed from the hierarchy. Look at the below example code. 

    Using ancestorRemoved method to Remove the Button from the Frame and Inform the Label

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    package AlixaProDev.EventListeners;
    
    import com.sun.security.auth.module.JndiLoginModule;
    
    import javax.swing.*;
    import javax.swing.event.AncestorEvent;
    import javax.swing.event.AncestorListener;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class AncestorListenerDemo  {
        JLabel label = new JLabel();
        JButton button = new JButton("remmove me");
        JFrame frame = new JFrame("AlixaProDev");
        JPanel panel = new JPanel();
        AncestorListenerDemo(){
            panel.add(button);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed (ActionEvent e) {
                    if (e.getSource()==button)
                        frame.remove(panel);
                }
            });
    
    
            frame.setLayout(new BorderLayout());
            frame.setSize(new Dimension(400,400));
            frame.add(label,BorderLayout.CENTER);
            frame.add(panel,BorderLayout.NORTH);
            frame.setVisible(true);
    
    
            button.addAncestorListener(new AncestorListener() {
                @Override
                public void ancestorAdded (AncestorEvent event) {
                }
    
                @Override
                public void ancestorRemoved (AncestorEvent event) {
                    label.setText("button Removed successfully");
                }
    
                @Override
                public void ancestorMoved (AncestorEvent event) {
                }
            });
        }
    
        public static void main (String[] args) {
            new AncestorListenerDemo();
        }
    
    }
    


Conclusion and Summary

Use the Ancestor Listener where you want to inform the other component whether a component or its parent component called the ancestor component is added or removed, set visible or invisible. In our last example, we have removed the panel to which we have added our button, and we informed the label by setting the size. The method was called when we click on the button, and the panel was removed and the method was called. Let me paste a picture of the code that i have written so far.

Java Swing tutorial



Have any questions in mind? Leave them in the comment section, I will answer them as soon as possible.

Post a Comment

Previous Post Next Post