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

Action.ACCELERATOR_KEY and Action.MNEMONIC_KEY are not used by JMenuItem

XMLWordPrintable

    • beta
    • generic
    • generic

      Name: mc57594 Date: 01/11/2000

      java version "1.3beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
      Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)

      Action.ACCELERATOR_KEY and Action.MNEMONIC_KEY are not used by JMenuItem

      If you create a JMenuItem using an action say
          new JMenuItem(MyAction)

      JMenuItem does not use the Action.ACCELERATOR_KEY and
      Action.MNEMONIC_KEY keys of the Action to set the menu
      item's accelerator key and mnemonic key. JMenuItem uses
      configurePropertiesFromAction to initialize stuff.
      Notice it does not look for Action.ACCELERATOR_KEY and
      Action.MNEMONIC_KEY.

      public class JMenuItem
       
      protected void configurePropertiesFromAction(Action a) {
        setText((a!=null?(String)a.getValue(Action.NAME):null));
        setIcon((a!=null?(Icon)a.getValue(Action.SMALL_ICON):null));
        setEnabled((a!=null?a.isEnabled():true));
      }

      You may want to check for this oversight in other classes
      which use configurePropertiesFromAction. Thanks

      =====================
      Let me know if this is what you need. Thanks

      Notice that we set the mneumonic in the following line
          putValue(AbstractAction.MNEMONIC_KEY, KeyStroke.getKeyStroke('D'));
      However, it is never used since
          JMenuItem.configurePropertiesFromAction(Action a)
      never looks for Action.ACCELERATOR_KEY and Action.MNEMONIC_KEY
      even though it should for both. Let me know if you need more information.
      I didn't code for the Action.ACCELERATOR_KEY in my example code.

      I look forward to your response. Thanks

      John Petrula
      ###@###.###
      ============================
      /*
      Problem: Action.ACCELERATOR_KEY and Action.MNEMONIC_KEY
               are not used by JMenuItem

      Setup: java.version 1.3 beta and 1.2.2
               Windows NT 4.0 SP4

      java version "1.3beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3beta-O)
      Java(TM) HotSpot Client VM (build 1.3beta-O, mixed mode)

      Background:

      If you create a JMenuItem using an action say
       new JMenuItem(MyAction)
      JMenuItem does not use the Action.ACCELERATOR_KEY and
      Action.MNEMONIC_KEY keys of the Action to set the menu
      item's accelerator key and mnemonic key. JMenuItem uses
      configurePropertiesFromAction to initialize stuff.
      Notice it does not look for Action.ACCELERATOR_KEY and
      Action.MNEMONIC_KEY.

      public class JMenuItem

      protected void configurePropertiesFromAction(Action a) {
       setText((a!=null?(String)a.getValue(Action.NAME):null));
       setIcon((a!=null?(Icon)a.getValue(Action.SMALL_ICON):null));
       setEnabled((a!=null?a.isEnabled():true));
      }

      You may want to check for this oversight in other classes
      which use configurePropertiesFromAction. Thanks
      */

      //Author: John Petrula
      //Company: Z-FAST
      //Description: ###@###.###

      package com.petrula.bug.Mnemonic;

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

      public class Mnemonic extends JFrame {

        DeleteFolderAction m_actionDeleteFolder =
          new DeleteFolderAction("Delete Folder");

        JMenuItem m_menuitemDeleteFolder = new JMenuItem(m_actionDeleteFolder);

        JMenuBar menuBar1 = new JMenuBar();
        JMenu menuFile = new JMenu();
        BorderLayout borderLayout1 = new BorderLayout();

        //Construct the frame
        public Mnemonic() {
          enableEvents(AWTEvent.WINDOW_EVENT_MASK);
          try {
            jbInit();
          }
          catch(Exception e) {
            e.printStackTrace();
          }
        }

        //Component initialization
        private void jbInit() throws Exception {
          this.getContentPane().setLayout(borderLayout1);
          this.setSize(new Dimension(400, 300));
          this.setTitle("Frame Title");
          menuFile.setText("File");
          menuFile.add(m_menuitemDeleteFolder);
          menuBar1.add(menuFile);
          this.setJMenuBar(menuBar1);
        }

        class DeleteFolderAction extends AbstractAction {

          DeleteFolderAction() {
            super();
          }
          DeleteFolderAction(String str) {
            super(str);
            putValue(AbstractAction.MNEMONIC_KEY, KeyStroke.getKeyStroke('D'));
          }
          public DeleteFolderAction(String name, Icon icon) {
            this(name);
          }

          public void actionPerformed(ActionEvent e) {
          }
        }

       public static void main(String[] args) {
          try {
            // Windows L&F
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

            // Create JFrame
            Mnemonic myFrame = new Mnemonic();

            myFrame.addWindowListener(new java.awt.event.WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                System.exit(0);
              }
            });

            myFrame.setSize(400,400);
            myFrame.setVisible(true);
          }
          catch( Exception e) {
            e.printStackTrace();
          }
        }
      }

      (Review ID: 98901)
      ======================================================================

            peterz Peter Zhelezniakov
            mchamnessunw Mark Chamness (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: