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