Example usage for java.awt Component addHierarchyListener

List of usage examples for java.awt Component addHierarchyListener

Introduction

In this page you can find the example usage for java.awt Component addHierarchyListener.

Prototype

public void addHierarchyListener(HierarchyListener l) 

Source Link

Document

Adds the specified hierarchy listener to receive hierarchy changed events from this component when the hierarchy to which this container belongs changes.

Usage

From source file:Main.java

/**
 * Adds a listener to the window parent of the given component. Can be
 * before the component is really added to its hierachy.
 * @param source The source component/*from   w w  w  .  j ava 2s.c  om*/
 * @param listener The listener to add to the window
 */
public static void addWindowListener(final Component source, final WindowListener listener) {
    if (source instanceof Window) {
        ((Window) source).addWindowListener(listener);
    } else {
        source.addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
                    SwingUtilities.getWindowAncestor(source).addWindowListener(listener);
                }
            }
        });
    }
}

From source file:Main.java

public static void addWindowListener(final Component source, final WindowListener listener) {
    if (source instanceof Window) {
        ((Window) source).addWindowListener(listener);
    } else {//from   w ww . java  2s .  com
        source.addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
                    SwingUtilities.getWindowAncestor(source).addWindowListener(listener);
                }
            }
        });
    }
}

From source file:Main.java

private static void addComponentListeners(Component c, Object... objs) {
    if (c == null)
        return;/*w w w.  j a  v  a  2  s. co  m*/
    ComponentListener componentListener = search(objs, ComponentListener.class);
    FocusListener focusListener = search(objs, FocusListener.class);
    HierarchyBoundsListener hierarchyBoundsListener = search(objs, HierarchyBoundsListener.class);
    HierarchyListener hierarchyListener = search(objs, HierarchyListener.class);
    InputMethodListener inputMethodListener = search(objs, InputMethodListener.class);
    KeyListener keyListener = search(objs, KeyListener.class);
    MouseListener mouseListener = search(objs, MouseListener.class);
    MouseMotionListener mouseMotionListener = search(objs, MouseMotionListener.class);
    MouseWheelListener mouseWheelListener = search(objs, MouseWheelListener.class);
    if (componentListener != null)
        c.addComponentListener(componentListener);
    if (focusListener != null)
        c.addFocusListener(focusListener);
    if (hierarchyBoundsListener != null)
        c.addHierarchyBoundsListener(hierarchyBoundsListener);
    if (hierarchyListener != null)
        c.addHierarchyListener(hierarchyListener);
    if (inputMethodListener != null)
        c.addInputMethodListener(inputMethodListener);
    if (keyListener != null)
        c.addKeyListener(keyListener);
    if (mouseListener != null)
        c.addMouseListener(mouseListener);
    if (mouseMotionListener != null)
        c.addMouseMotionListener(mouseMotionListener);
    if (mouseWheelListener != null)
        c.addMouseWheelListener(mouseWheelListener);
}

From source file:org.monkeys.gui.PopupWindow.java

public PopupWindow(final Frame parent, final Component base) {
    super(parent);

    if (null == base) {
        throw new NullArgumentException("component");
    }/* w  ww  . ja v a2  s. co  m*/

    this.addWindowFocusListener(new WindowAdapter() {
        @Override
        public void windowLostFocus(WindowEvent e) {
            hidePopup();
        }
    });
    base.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            hidePopup();
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            hidePopup();
        }

        @Override
        public void componentHidden(ComponentEvent e) {
            hidePopup();
        }
    });
    base.addHierarchyListener(new HierarchyListener() {
        @Override
        public void hierarchyChanged(HierarchyEvent e) {
            hidePopup();
        }
    });
}

From source file:com.mac.tarchan.desktop.event.EventQuery.java

/**
 * ???????????//  w w w  .  j a  va2s. co  m
 * 
 * @param hierarchyChanged ?
 * @return ??
 * @see <a href="http://terai.xrea.jp/Swing/DefaultFocus.html ">Window?????? - Java Swing Tips</a>
 * @see HierarchyListener#hierarchyChanged(HierarchyEvent)
 */
public EventQuery ready(final HierarchyListener hierarchyChanged) {
    for (Component child : list) {
        child.addHierarchyListener(new HierarchyListener() {
            public void hierarchyChanged(HierarchyEvent e) {
                if (e.getChangeFlags() == HierarchyEvent.DISPLAYABILITY_CHANGED)
                    hierarchyChanged.hierarchyChanged(e);
            }
        });
    }
    return this;
}

From source file:self.philbrown.javaQuery.$.java

/**
 * Registers common component changes//from  ww  w .ja  v  a  2  s .  c  o m
 * @param function receives an instance of javaQuery with the changed view selected, as well as the
 * following arguments:
 * <ol>
 * <li>{@link changeEvent} to define the type of event that was triggered
 * <li>The associated event
 * </ol>
 * @return
 * @see changeEvent
 */
public $ change(final Function function) {
    for (final Component component : views) {
        component.addInputMethodListener(new InputMethodListener() {

            @Override
            public void caretPositionChanged(InputMethodEvent event) {
                function.invoke($.with(component), ChangeEvent.INPUT_CARET_POSITION_CHANGED, event);
            }

            @Override
            public void inputMethodTextChanged(InputMethodEvent event) {
                function.invoke($.with(component), ChangeEvent.INPUT_METHOD_TEXT_CHANGED, event);
            }

        });

        component.addHierarchyBoundsListener(new HierarchyBoundsListener() {

            @Override
            public void ancestorMoved(HierarchyEvent event) {
                function.invoke($.with(component), ChangeEvent.HIERARCHY_ANCESTOR_MOVED, event);
            }

            @Override
            public void ancestorResized(HierarchyEvent event) {
                function.invoke($.with(component), ChangeEvent.HIERARCHY_ANCENSTOR_RESIZED, event);
            }

        });

        component.addHierarchyListener(new HierarchyListener() {

            @Override
            public void hierarchyChanged(HierarchyEvent event) {
                function.invoke($.with(component), ChangeEvent.HIERARCHY_CHANGED, event);
            }

        });

        component.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent event) {
                function.invoke($.with(component), ChangeEvent.PROPERTY_CHANGED, event);
            }
        });

        component.addComponentListener(new ComponentListener() {

            @Override
            public void componentHidden(ComponentEvent event) {
                function.invoke($.with(component), ChangeEvent.COMPONENT_HIDDEN, event);
            }

            @Override
            public void componentMoved(ComponentEvent event) {
                function.invoke($.with(component), ChangeEvent.COMPONENT_MOVED, event);
            }

            @Override
            public void componentResized(ComponentEvent event) {
                function.invoke($.with(component), ChangeEvent.COMPONENT_RESIZED, event);
            }

            @Override
            public void componentShown(ComponentEvent event) {
                function.invoke($.with(component), ChangeEvent.COMPONENT_SHOWN, event);
            }

        });

        if (component instanceof Container) {
            ((Container) component).addContainerListener(new ContainerListener() {

                @Override
                public void componentAdded(ContainerEvent event) {
                    function.invoke($.with(component), ChangeEvent.COMPONENT_ADDED, event);
                }

                @Override
                public void componentRemoved(ContainerEvent event) {
                    function.invoke($.with(component), ChangeEvent.COMPONENT_REMOVED, event);
                }

            });
        }
    }
    return this;
}