title: How to detect TAB key-press in JComboBox?
body: <p>I am trying to find out how to detect if a JComboBox lost focus when user pressed tab or via a mouse-click outside the component's area.</p>
  
  <p>Adding a FocusListener to the editor component of the JComboBox does not help me, as I can't find out if user used the mouse or moved the focus via the tab key. Any ideas would be greatly appreciated.</p>
  
  <p>Edit 1:
  What I am trying to achieve is this:</p>
  
  <ul>
  <li>Suppose user drops down the list (JComboBox popupMenu shows up), and navigates via cursor keys...</li>
  <li>Case 1: user presses tab. In this case, I want to cut off some of the information from the item and show only some parts.</li>
  <li>Case 2: user clicks with the mouse outside the popupMenu's area (here we have subcases, but they all fall into the same category). In this case I want to change JComboBox to show the previously edited item, not what user navigated...</li>
  <li>There are several other cases (mouse item pick, the enter key, escape, etc.). I could easily handle these, but detecting tab is tricky because I can't catch this event as it is handled by FocusManager.</li>
  </ul>
  
  <p>Edit 2:
  It seems that I have to use <code>setFocusTraversalKeysEnabled(false)</code> to get notified when TAB is pressed, and when i capture that event, I should manually transfer focus... I do not like this solution, but that is so far the best I could come up with.</p>
  
  <p>Solution:</p>
  
  <p>Following piece of Java code is actually solving my problem. As I wrote in <em>Edit 2</em> the easiest solution was to disable focus traversal. I shamelessly borrowed Kleopatra's code, and all works now. :)</p>
  
  <pre><code>    if (!isTableCellEditor()) {
          comboBoxEditor.setFocusTraversalKeysEnabled(false);
  
          Action myAction = new AbstractAction() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  handleTabPress();
                  comboBoxEditor.transferFocus();
              } // actionPerformed() method
          };
  
          comboBoxEditor.getActionMap().put("tab-action", myAction);
          comboBoxEditor.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
              .put(KeyStroke.getKeyStroke("TAB"), "tab-action");
      } // if
  </code></pre>
  
  <p>Thanks to all participants in the discussion!</p>