Deactivate selection by letter in JList

点点圈 提交于 2019-12-11 09:47:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!