Example usage for java.awt Container addContainerListener

List of usage examples for java.awt Container addContainerListener

Introduction

In this page you can find the example usage for java.awt Container addContainerListener.

Prototype

public synchronized void addContainerListener(ContainerListener l) 

Source Link

Document

Adds the specified container listener to receive container events from this container.

Usage

From source file:ContainerTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    ContainerListener cont = new ContainerListener() {
        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Selected: " + e.getActionCommand());
            }/*from w  ww.ja v a  2s  .c om*/
        };

        public void componentAdded(ContainerEvent e) {
            Component c = e.getChild();
            if (c instanceof JButton) {
                JButton b = (JButton) c;
                b.addActionListener(listener);
            }
        }

        public void componentRemoved(ContainerEvent e) {
            Component c = e.getChild();
            if (c instanceof JButton) {
                JButton b = (JButton) c;
                b.removeActionListener(listener);
            }
        }
    };

    contentPane.addContainerListener(cont);

    contentPane.setLayout(new GridLayout(3, 2));
    contentPane.add(new JButton("First"));
    contentPane.add(new JButton("Second"));
    contentPane.add(new JButton("Third"));
    contentPane.add(new JButton("Fourth"));
    contentPane.add(new JButton("Fifth"));

    frame.setSize(300, 200);
    frame.show();
}

From source file:Main.java

private static void addContainerListeners(Container c, Object... objs) {
    if (c == null)
        return;//from  w ww .ja  v  a  2  s .c  o  m
    addComponentListeners(c, objs);
    ContainerListener containerListener = search(objs, ContainerListener.class);
    PropertyChangeListener propertyChangeListener = search(objs, PropertyChangeListener.class);
    if (containerListener != null)
        c.addContainerListener(containerListener);
    if (propertyChangeListener != null)
        c.addPropertyChangeListener(propertyChangeListener);
}

From source file:org.eclipse.jubula.rc.swing.components.AUTSwingHierarchy.java

/**
 * Register the AutHierarchy as a container listener to <code>container</code>.
 * @param container the container to register to
 *///from w w w  .  j a  v a 2s .c o m
private void registerAsContainerListener(final Container container) {
    Runnable registrationRunnable = new Runnable() {
        public void run() {
            if (log.isInfoEnabled()) {
                log.info("registering as listener to container " + container); //$NON-NLS-1$               
            }

            ContainerListener[] listener = container.getContainerListeners();
            for (int i = 0; i < listener.length; i++) {
                if (listener[i] instanceof AUTSwingHierarchy) {
                    return;
                }
            }
            container.addContainerListener(AUTSwingHierarchy.this);
        }
    };

    registerListener(registrationRunnable);

}