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

MenuShortcut does not produce correct event with CheckboxMenuItem


    • Subcomponent:
    • CPU:
    • OS:
      windows_95, windows_98


      Name: mf23781 Date: 07/15/98

      /* Although menu shortcuts are defined for the two checkbox
       * menu items, they are not displayed and itemStateChanged events
       * are not broadcast when the shortcuts are used. actionPerformed
       * events are broadcast (arguably incorrectly). In my opinion,
       * regardless of how a checkbox menu item is selected (mouse click
       * or keyboard shortcut), the same event(s) should be received.
       * CTL+1 & CTL+2 to use the keyboard shortcut.
      import java.awt.*;
      import java.awt.event.*;

      public class Bugs extends Frame
                               implements ActionListener, ItemListener
        // menus
        private MenuBar MB = new MenuBar();
        private CBMenu CBM = new CBMenu();

        public Bugs()
        { super("");
          addWindowListener(new LocalWindowAdapter());

          // build the menu bar

          // layout the client area

        public void itemStateChanged(ItemEvent evt)
          System.out.println("itemStateChanged(ItemEvent) called");
          if (evt.getSource() instanceof CheckboxMenuItem)
          { CheckboxMenuItem mi = (CheckboxMenuItem)evt.getSource();
            if (evt.getStateChange() == ItemEvent.SELECTED)
              System.out.println("CheckboxMenuItem "+mi.getLabel()+" selected.");
              System.out.println("CheckboxMenuItem "+mi.getLabel()+" deselected.");

        public void actionPerformed(ActionEvent evt)
          System.out.println("actionPerformed(ActionEvent) called");
          if (evt.getSource() instanceof MenuItem)
          { MenuItem mi = (MenuItem)evt.getSource();
            System.out.println("MenuItem "+mi.getLabel()+" selected.");

        private void addMenuBarListeners(MenuBar MB)
        { for (int i = 0; i < MB.getMenuCount(); i++)
          { addMenuListeners(MB.getMenu(i)); }

        private void addMenuListeners(Menu menu)
        { for (int i = 0; i < menu.getItemCount(); i++)
          { MenuItem item = menu.getItem(i);
            if (item instanceof Menu)
            else if (item instanceof CheckboxMenuItem)
            { ((CheckboxMenuItem)item).addItemListener(this);

        class LocalWindowAdapter extends WindowAdapter
        { public void windowClosing(WindowEvent evt) { System.exit(0); } }

        class CBMenu extends Menu
        { protected Menu CBs = new Menu("Numbers");
          protected MenuShortcut msCB1 = new MenuShortcut(KeyEvent.VK_1),
                                     msCB2 = new MenuShortcut(KeyEvent.VK_2);
          protected CheckboxMenuItem miCB1 = new CheckboxMenuItem("One",false),
                                     miCB2 = new CheckboxMenuItem("Two",false);
          protected CBMenu()
          { super();

        public static void main(String argv[])
        { (new Bugs()).setVisible(true); }

      Name: krT82822 Date: 12/10/99

      (see also # 4095509)

      C:\MrcProgs\Java\Classes>java -version
      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)


      Here is a section of the code which is apparently ignored. Although
      CheckboxMenuItem is a subclass of MenuItem the MenuItem method
      setShorcut(MenuShortcut s) does not appear to 'take', although the MenuItem
      method setLabel(String label) does 'take'.

                         {CheckboxMenuItem MI;
                          int keyNo=0;
                          String ShortcutTranslation;
                          MI=new CheckboxMenuItem(MenuTranslation); // attach
                          MI=new CheckboxMenuItem(); // attach
                          MI.addItemListener(f); // menu item
                          if (newMenuItem.endsWith("*"))

      //** The shortcut doesn't appear to work with CheckboxMenuItems, but it ought
      //** to as it has inherited all MenuItem's methods - see setLabel above

                          catch(MissingResourceException re)
                             {ShortcutTranslation=""; // and, if present,
                          if (ShortcutTranslation!="")
                             {MI.setShortcut(new MenuShortcut // its shortcut
                          oldMenu.add(MI); // to its parent
                          MI.setActionCommand(newMenuItem); // will be action tag
      (Review ID: 98871)


          Issue Links



              • Assignee:
                rkhansunw Robi Khan (Inactive)
                miflemi Mick Fleming (Inactive)
              • Votes:
                0 Vote for this issue
                0 Start watching this issue


                • Created: