Java tutorial
//package com.java2s; import java.awt.Component; import java.util.EventListener; import javax.swing.event.EventListenerList; public class Main { /** * Checks if the listener is always registered to the EventListenerList to avoid duplicated registration of the same listener * * @param list the EventListenerList to register the listener. * @param t the type of the EventListener. * @param l the listener. * @return true if already registered. Otherwise false. */ public static boolean isListenerRegistered(EventListenerList list, Class t, EventListener l) { Object[] objects = list.getListenerList(); return isListenerRegistered(objects, t, l); } /** * 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. * @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); } private static boolean isListenerRegistered(Object[] objects, Class t, EventListener l) { for (int i = 0; i < objects.length; i++) { Object listener = objects[i]; if (t.isAssignableFrom(listener.getClass()) && (listener == l)) { return true; } } return false; } }