Tabbing In and Out of an Editable JComboBox
Tabbing in and out of an editable JComboBox seems to be really quirky. First we need to disable the default focus traversal keys for the JComboBox editor.
The bottom line is this works!
editor.setFocusTraversalKeysEnabled(false);Then we add a KeyListener as below :
editor.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_TAB: int n = e.getModifiers(); if (n > 0) { comboBox.transferFocusBackward(); } else { comboBox.getEditor().getEditorComponent().transferFocus(); } e.consume(); break; } } });Notice that to tab forward, we are using the editor component and to tab backward we are using the combobox directly. Any clarifications as to why this happens are most welcome.
The bottom line is this works!
Comments
Post a Comment