protected boolean processKeyBinding( javax.swing.KeyStroke ks, java.awt.event.KeyEvent e, int condition, boolean pressed) { // live search in current parent node if ((Character.isLetterOrDigit(e.getKeyChar())) && ks.isOnKeyRelease()) { char keyChar = e.getKeyChar(); // search term String search = ("" + keyChar).toLowerCase(); // try to find node with matching searchterm plus the search before int nextIndexMatching = getNextIndexMatching(cachedSearchKey + search); // if we did not find anything, try to find search term only: restart! if (nextIndexMatching < 0) { nextIndexMatching = getNextIndexMatching(search); cachedSearchKey = ""; } // if we found a node, select it, make it visible and return true if (nextIndexMatching >= 0) { // store found treepath cachedSearchKey = cachedSearchKey + search; setSelectedIndex(nextIndexMatching); return true; } cachedSearchKey = ""; return true; } return super.processKeyBinding(ks, e, condition, pressed); }
/** * This is hack. AWT doesn't allow to create KeyStroke with specified key code and key char * simultaneously. Therefore we are using reflection. */ private static KeyStroke getKeyStrokeWithoutMouseModifiers(KeyStroke originalKeyStroke) { int modifier = originalKeyStroke.getModifiers() & ~InputEvent.BUTTON1_DOWN_MASK & ~InputEvent.BUTTON1_MASK & ~InputEvent.BUTTON2_DOWN_MASK & ~InputEvent.BUTTON2_MASK & ~InputEvent.BUTTON3_DOWN_MASK & ~InputEvent.BUTTON3_MASK; try { Method[] methods = AWTKeyStroke.class.getDeclaredMethods(); Method getCachedStrokeMethod = null; for (Method method : methods) { if (GET_CACHED_STROKE_METHOD_NAME.equals(method.getName())) { getCachedStrokeMethod = method; getCachedStrokeMethod.setAccessible(true); break; } } if (getCachedStrokeMethod == null) { throw new IllegalStateException("not found method with name getCachedStrokeMethod"); } Object[] getCachedStrokeMethodArgs = new Object[] { originalKeyStroke.getKeyChar(), originalKeyStroke.getKeyCode(), modifier, originalKeyStroke.isOnKeyRelease() }; return (KeyStroke) getCachedStrokeMethod.invoke(originalKeyStroke, getCachedStrokeMethodArgs); } catch (Exception exc) { throw new IllegalStateException(exc.getMessage()); } }