List of usage examples for javax.swing JPopupMenu addKeyListener
public synchronized void addKeyListener(KeyListener l)
From source file:edu.ku.brc.ui.UIHelper.java
/** * Adds a special key listener to process a RETURN key for selecting and item * in the popup list. This is installed only for Windows and Linux. * @param popupMenu the popup menu to be altered. *///w w w . j a v a2 s . c om public static void addSpecialKeyListenerForPopup(final JPopupMenu popupMenu) { if (!UIHelper.isMacOS()) { popupMenu.addKeyListener(new KeyAdapter() { //@Override public void keyPressed(KeyEvent e) { super.keyPressed(e); if (e.getKeyCode() == VK_ENTER) { for (int i = 0; i < popupMenu.getComponentCount(); i++) { Component c = popupMenu.getComponent(i); if (c instanceof JMenuItem) { JMenuItem mi = (JMenuItem) c; if (mi.isArmed()) { mi.doClick(); popupMenu.setVisible(false); } } } } } }); } }