问题
I've got a JList list and the following code line:
list.getInputMap().put(KeyStroke.getKeyStroke('d'), "action");
So when my list is in focus and I press the d key on my keyboard an action should be performed. That action takes into account which item of my JList is currently selected. The problem is that whenever there is an item in my list with first letter 'd' my selection will jump to that entry first and then do the action (applied to the wrong item).
So my question is: How do I disable those selections jumps in JLists caused by typing a letter?
回答1:
You can remove the KeyListeners from the JList.
I tried but couldn't figure out what this breaks in terms of standard functionality.
KeyListener[] lsnrs = list.getKeyListeners();
for (int i = 0; i < lsnrs.length; i++) {
list.removeKeyListener(lsnrs[i]);
}
来源:https://stackoverflow.com/questions/34251765/deactivate-selection-by-letter-in-jlist