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.
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

Popular posts from this blog

How to Convert an ImageIcon to a Base64 encoded String in Java

How to install a network Star TSP700II installed on Windows XP in Windows 10

Problems when generating and uploading JSON from CSV for GSTR1