List of usage examples for javax.swing JComboBox getKeyListeners
public synchronized KeyListener[] getKeyListeners()
From source file:imageuploader.ImgWindow.java
private void setExplicitSelectionManager(JComboBox comboBox) { class ExplicitSelectionManager implements KeyListener, FocusListener { private JComboBox src; private KeyListener superKeyListener; ExplicitSelectionManager(JComboBox src) { this.src = src; // we like what the default key listener does, but not the action command // it uses for ActionEvents it fires for plain text type-ahead characters this.superKeyListener = src.getKeyListeners()[0]; // we only have one src.removeKeyListener(superKeyListener); // will be replace right away, below }//from w w w .j a va2 s .co m @Override public void keyTyped(KeyEvent e) { // basic combo box has no code in keyTyped } @Override public void keyPressed(KeyEvent e) { // in the default JComboBox implementation, the KeySelectionManager is // called from keyPressed. I'm fine with the implementation of // the default, but I don't want it firing ActionEvents that will cause // model updates src.setActionCommand("comboBoxMovement"); this.superKeyListener.keyPressed(e); src.setActionCommand("comboBoxChanged"); if (e.getKeyCode() == 10) { src.setSelectedIndex(src.getSelectedIndex()); } } @Override public void keyReleased(KeyEvent e) { // basic combo box has no code in keyReleased } @Override public void focusGained(FocusEvent e) { } @Override // this will also give us the event we want, if the user decides to Tab out of // the combo box, instead of hitting Enter public void focusLost(FocusEvent e) { src.setSelectedIndex(src.getSelectedIndex()); } } ExplicitSelectionManager newSelectionManager = new ExplicitSelectionManager(comboBox); comboBox.addKeyListener(newSelectionManager); comboBox.addFocusListener(newSelectionManager); }