Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4791569

Accelerator keys don't work when JPopupMenu is outside its parent

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.2_07
    • 1.4.1, 1.4.2_05
    • client-libs
    • b01
    • generic, x86
    • generic, windows_2000

        Name: jk109818 Date: 12/10/2002


        FULL PRODUCT VERSION :
        java version "1.4.1_01"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4
        Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

        FULL OPERATING SYSTEM VERSION :
        Microsoft Windows 2000 [Version 5.00.2195]

        A DESCRIPTION OF THE PROBLEM :
        JPopupMenuItem accelerator keys (set thanks Command) are
        nolonger active as soon as the JPopupMenu is (even
        partially) outside the listener area.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1.Right click the mouse button to trigger a PopupMenu so the
        popup is partially outside its parent
        2.Try to type an accelerator command
        3.The command is not effective

        (note that the keyboard arrows are still effective)

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        The accelerator command should be effective

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package sample.popup;

        import java.awt.BorderLayout;
        import java.awt.event.ActionEvent;
        import java.awt.event.KeyEvent;
        import java.awt.event.MouseAdapter;
        import java.awt.event.MouseEvent;

        import javax.swing.AbstractAction;
        import javax.swing.Action;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JList;
        import javax.swing.JOptionPane;
        import javax.swing.JPanel;
        import javax.swing.JPopupMenu;
        import javax.swing.JScrollPane;
        import javax.swing.KeyStroke;

        /**
         * @author f.savino
         *
         * To change this generated comment edit the template variable "typecomment":
         * Window>Preferences>Java>Templates.
         * To enable and disable the creation of type comments go to
         * Window>Preferences>Java>Code Generation.
         */

        public class PopupSample extends JPanel {
        private final String _lstModel[] = { "First Item", "Second Item", "Third Item" };
        private JList _myList = null;
        private JScrollPane _jsp = null;
        private JPopupMenu _myPopupMenu = null;
        private Action _actCut = null;
        private Action _actCopy = null;
        private Action _actPaste = null;

        public PopupSample() {
        initMembers();
        initLayout();
        initListener();
        }

        /**
        * Method initMembers.
        */
        private void initMembers() {
        _myList = new JList(_lstModel);
        _myPopupMenu = new JPopupMenu("MyPopup");
        // Cut menu item
        _actCut = new AbstractAction("cut") {
        public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(PopupSample.this.getTopLevelAncestor(), "Cut");
        }
        };
                _actCut.putValue(Action.ACCELERATOR_KEY,
        KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
                _myPopupMenu.add(_actCut);
                
        // Copy menu item
        _actCopy = new AbstractAction("Copy") {
        public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(PopupSample.this.getTopLevelAncestor(), "Copy");
        }
        };
                _actCopy.putValue(Action.ACCELERATOR_KEY,
        KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
                _myPopupMenu.add(_actCopy);

        // Paste menu item
        _actPaste = new AbstractAction("Paste") {
        public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(PopupSample.this.getTopLevelAncestor(), "Paste");
        }
        };
                _actPaste.putValue(Action.ACCELERATOR_KEY,
        KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
                _myPopupMenu.add(_actPaste);
                
                _jsp = new JScrollPane(_myList);
        }


        /**
        * Method initLayout.
        */
        private void initLayout() {
        this.setLayout(new BorderLayout());
        this.add(new JLabel("Right click in the control above..."), BorderLayout.NORTH);
        this.add(_jsp, BorderLayout.CENTER);
        }

        /**
        * Method initListener.
        */
        private void initListener() {
        _myList.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
        }
        public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
        }
        private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
        _myPopupMenu.show(e.getComponent(), e.getX(), e.getY());
        }
        }
        });
        _jsp.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
        }
        public void mouseReleased(MouseEvent e) {
        maybeShowPopup(e);
        }
        private void maybeShowPopup(MouseEvent e) {
        if (e.isPopupTrigger()) {
        _myPopupMenu.show(e.getComponent(), e.getX(), e.getY());
        }
        }
        });
        }

        public static void main(String[] args) {
        JFrame frm = new JFrame();
        frm.setTitle("ContextMenu");
        frm.getContentPane().add(new PopupSample());
        // Not 1.2 complient !!!
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.pack();
        frm.show();
        }
        }

        ---------- END SOURCE ----------
        (Review ID: 178961)
        ======================================================================
        ###@###.### 10/13/04 17:04 GMT

              peterz Peter Zhelezniakov
              jkimsunw Jeffrey Kim (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: