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

Default button responds to CTRL-ENTER while popup menu is active.

XMLWordPrintable

    • b53
    • x86
    • windows_2000

      Name: rmT116609 Date: 05/28/2003


      FULL PRODUCT VERSION :
      java version "1.4.2-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b19)
      Java HotSpot(TM) Client VM (build 1.4.2-beta-b19, mixed mode)

      FULL OS VERSION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      When a JPopupMenu is displayed, an underlying Default JButton still responds to the CTRL-ENTER keystroke.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the test applet (below).
      Click the "Popup" button.
      (The popup menu is displayed.)
      Type CTRL-ENTER while the popup menu is still active.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I expect CTRL-ENTER to be ignored while the popup menu is active.
      ACTUAL -
      The underlying "Default" button visibly "clicks".
      The console output shows that the "Default" button produced an ActionEvent in response to CTRL-ENTER.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Console output:
      actionPerformed: JButton: Popup
      actionPerformed: JButton: Default

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      // Applet demonstrates JPopupMenu/Default-JButton bug.

      import java.awt.event.*;
      import javax.swing.*;

      public class JApplet1 extends JApplet implements ActionListener
      {
        private JButton btnPopup = new JButton("Popup");
        private JButton btnDefault = new JButton("Default");
        private JPopupMenu popupMenu = new JPopupMenu();

        public void init()
        {
          // Create 2 buttons
          getContentPane().setLayout(null);
          setSize(260,130);
          getContentPane().add(btnPopup);
          btnPopup.setBounds(20,50,100,40);
          getContentPane().add(btnDefault);
          btnDefault.setBounds(140,50,100,40);

          btnPopup.addActionListener(this);
          btnDefault.addActionListener(this);

          getRootPane().setDefaultButton(btnDefault);

          // Build popup menu
          for (int m=1; m <= 6; m++) {
            JMenuItem item = new JMenuItem("Item" + m);
            popupMenu.add(item);
            item.addActionListener(this);
          }
        }

        public void actionPerformed(ActionEvent e)
        {
          System.out.println("actionPerformed: " + describeComponent(e.getSource()));
          if (e.getSource() == btnPopup) {
            popupMenu.show(btnPopup, -(btnPopup.getWidth()/2), -(btnPopup.getHeight()/2));
          }
        }

        private String describeComponent(Object o)
        {
          if (o instanceof JButton) return "JButton: " + ((JButton)o).getText();
          if (o instanceof JMenuItem) return "JMenuItem: " + ((JMenuItem)o).getText();
          return o.getClass().getName();
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Call the following function on every JMenu, JPopupMenu, JMenuItem and JCheckBoxMenuItem:

        // eatUpCtrlEnter
        // This is called on each menu component.
        // It causes the menu component to eat the CTRL-ENTER keystroke so that the
        // underlying default button is not activated.
        public static void eatUpCtrlEnter(JComponent jc)
        {
          jc.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
             CTRL_ENTER, doNothingCmd);
          jc.getActionMap().put(doNothingCmd, doNothingAction);
        }

        private static final String doNothingCmd = "doNothing";
        private static final KeyStroke CTRL_ENTER
          = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, Event.CTRL_MASK);
        private static final Action doNothingAction
           = new AbstractAction() { public void actionPerformed(ActionEvent e) {}};
      (Review ID: 186608)
      ======================================================================
      ###@###.### 10/12/04 11:56 GMT

            kizune Alexander Zuev
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: