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