Java tutorial
//package com.java2s; import java.awt.Component; import java.awt.event.MouseMotionListener; public class Main { /** * Inserts the mouse motion listener at the particular index in the listeners' chain. * * @param component * @param l * @param index */ public static void insertMouseMotionListener(Component component, MouseMotionListener l, int index) { MouseMotionListener[] listeners = component.getMouseMotionListeners(); for (MouseMotionListener listener : listeners) { component.removeMouseMotionListener(listener); } for (int i = 0; i < listeners.length; i++) { MouseMotionListener listener = listeners[i]; if (index == i) { component.addMouseMotionListener(l); } component.addMouseMotionListener(listener); } // inex is too large, add to the end. if (index > listeners.length - 1) { component.addMouseMotionListener(l); } } }