Example usage for java.awt Component getListeners

List of usage examples for java.awt Component getListeners

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public <T extends EventListener> T[] getListeners(Class<T> listenerType) 

Source Link

Document

Returns an array of all the objects currently registered as FooListeners upon this Component .

Usage

From source file:Main.java

/**
 * Checks if the listener is always registered to the Component to avoid duplicated registration of the same listener
 *
 * @param component the component that you want to register the listener.
 * @param t         the type of the EventListener.
 * @param l         the listener.//from ww w.  j  a v  a  2 s . c  o m
 * @return true if already registered. Otherwise false.
 */
public static boolean isListenerRegistered(Component component, Class t, EventListener l) {
    Object[] objects = component.getListeners(t);

    return isListenerRegistered(objects, t, l);
}

From source file:op.tools.SYSTools.java

/**
 * see: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4380536
 * Puh, das hier ist aus der Sun Bug Datenbank. Etwas krude... Ich hoffe
 * die lassen sich mal was besseres einfallen.
 *///from  w  w  w  .  j a v a2  s . co m
static private void removeListeners(Component comp) {
    Method[] methods = comp.getClass().getMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        String name = method.getName();
        if (name.startsWith("remove") && name.endsWith("Listener")) {

            Class[] params = method.getParameterTypes();
            if (params.length == 1) {
                EventListener[] listeners = null;
                try {
                    listeners = comp.getListeners(params[0]);
                } catch (Exception e) {
                    // It is possible that someone could create a listener
                    // that doesn't extend from EventListener.  If so,
                    // ignore it
                    OPDE.debug("Listener " + params[0] + " does not extend EventListener");
                    continue;
                }
                for (int j = 0; j < listeners.length; j++) {
                    try {
                        method.invoke(comp, new Object[] { listeners[j] });
                        //OPDE.debug("removed Listener " + name + "for comp " + comp + "\n");
                    } catch (Exception e) {
                        OPDE.debug("Cannot invoke removeListener method " + e);
                        // Continue on.  The reason for removing all listeners is to
                        // make sure that we don't have a listener holding on to something
                        // which will keep it from being garbage collected. We want to
                        // continue freeing listeners to make sure we can free as much
                        // memory has possible
                    }
                }
            } else {
                // The only Listener method that I know of that has more
                // one argument is removePropertyChangeListener.  If it is
                // something other than that, flag it and move on.
                if (!name.equals("removePropertyChangeListener")) {
                    OPDE.debug("    Wrong number of Args " + name);
                }
            }
        }
    }
}

From source file:org.eclipse.jubula.rc.swing.listener.ComponentHandler.java

/**
 * @param component the component to check for a listener.
 * @param listenerClass The class of listener for which to check.
 * @return <code>true</code> if an <code>AUTSwingHierarchy</code> is 
 *         registered as a <code>listenerClass</code> listener on the
 *         given <code>component</code>. Otherwise, <code>false</code>.
 *///from  ww  w.  j a va  2s . c  o  m
private boolean hasListener(Component component, Class<? extends EventListener> listenerClass) {

    EventListener[] listener = component.getListeners(listenerClass);
    int length = listener.length;
    for (int i = 0; i < length; i++) {
        if (listener[i] instanceof AUTSwingHierarchy) {
            return true;
        }
    }
    return false;
}