Reputation: 4316
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?
Upvotes: 2
Views: 521
Reputation: 135
Instead of removing the KeyListeners, override the getNextMatch()
method of JList to return -1 every time. Then it won't jump to the selection by letter on key press.
import javax.swing.JList;
import javax.swing.text.Position;
class CustomJList extends JList<Object> {
@Override
public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
return -1;
}
}
Upvotes: 0
Reputation: 2147
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]);
}
Upvotes: 2