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

MenuShortcut does not produce correct event with CheckboxMenuItem

XMLWordPrintable

    • x86
    • 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
          setMenuBar(MB);
          MB.add(CBM);
          addMenuBarListeners(MB);

          // layout the client area
          setSize(600,200);
        }

        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.");
            else
              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)
              addMenuListeners((Menu)item);
            else if (item instanceof CheckboxMenuItem)
            { ((CheckboxMenuItem)item).addItemListener(this);
              item.addActionListener(this);
            }
            else
              item.addActionListener(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();
            setLabel("CheckboxMenuItems");
            miCB1.setShortcut(msCB1);
            miCB2.setShortcut(msCB2);
            CBs.add(miCB1);
            CBs.add(miCB2);
            add(CBs);
          }
        }

        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)

      C:\MrcProgs\Java\Classes>


      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.setLabel(MenuTranslation);
                          MI.addItemListener(f); // menu item
                          if (newMenuItem.endsWith("*"))
                             {MI.setState(true);
                             }

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

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

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: