Example usage for javax.swing JPopupMenu addKeyListener

List of usage examples for javax.swing JPopupMenu addKeyListener

Introduction

In this page you can find the example usage for javax.swing JPopupMenu addKeyListener.

Prototype

public synchronized void addKeyListener(KeyListener l) 

Source Link

Document

Adds the specified key listener to receive key events from this component.

Usage

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);
                            }
                        }
                    }
                }
            }
        });
    }
}