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

Action commands generated by MenuShortcut is null unless setActionCommand used

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.2, 1.2.0
    • client-libs
    • None
    • x86, sparc
    • solaris_2.6, windows_nt

      The action command generated when a menu shortcut is entered is always null unless setActionCommand() is used. THis is because MenuItem.handleShortcut() uses actionCommand instead of getActionCommand() when creating the ActionEvent. The fix is to use getActionCommand() instead. THis way, the event generated is consistent with that generated by mouse actions over the same menu item.

      ==========================================================
      Another report of this problem with an example:

      A MenuShortcut added to a MenuItem fires the corresponding
      ActionEvent (on actionPerformed) but the call to getActionCommand() on the ActionEvent received returns null, resulting on the user being unable to identify the command launched.

      import java.awt.*;
      import java.awt.event.*;


      public
      class KeyTest
          extends Frame
          implements ActionListener
      {
      public
      KeyTest()
      {
          super("test");
          MenuBar mb = new MenuBar();
          Menu t = new Menu("test");
          MenuItem mt = new MenuItem("test 1", new MenuShortcut('s'));
          mt.addActionListener(this);
          t.add(mt);
          mt = new MenuItem("test 2", new MenuShortcut('x'));
          t.add(mt);
          mt.addActionListener(this);
          mb.add(t);
          setMenuBar(mb);
      }

      public
      void
      actionPerformed(ActionEvent e)
      {
          System.out.println(e.getActionCommand());
      }

      public static
      void
      main(String[] s)
      {
          KeyTest t = new KeyTest();
          t.setSize(200,200);
          t.show();
      }
      }

            msomlosunw Mike Somlo (Inactive)
            rleesunw Rosanna Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: