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

Last JMenu in JMenuBar selected instead of first

XMLWordPrintable

      A DESCRIPTION OF THE REQUEST :
      In a JMenuBar containing a first JMenu with the same Mnemonic as the last JMenu, the last JMenu is selected using the keys [Alt]+[<Mnemonic>] and not the first JMenu.
      Considering this a bug, I report this to you.
      The enhancement could be that the firstJMenu is selected instead of the last.


      JUSTIFICATION :
      It is disturbing and annoying to select a JMenu from a JMenuBar and to get a wrong one selected.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The first JMenu with a Mnemonic is selected instead of the last with the same Mnemonic.

      ACTUAL -
      In a JMenuBar containing a first JMenu with the same Mnemonic as the last JMenu, the last JMenu is selected using the keys [Alt]+[<Mnemonic>] and not the first JMenu.


      ---------- BEGIN SOURCE ----------
      // File with main()
      package menutest;

      import java.awt.BorderLayout;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;
      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.JTextArea;


      public class MenuTest extends JFrame {

      /**
       * generated serialVersionUID
       */
        private static final long serialVersionUID = 5727487056953216993L;

        MenuTest mtMe;

        String hEIconFileName; // The filename for the Icon
        
        MenuItemListener mil;
        
        static String theTitle; // " HTML Editor " ;
        
        // components and controls
        
        JMenuBar jmb01;
          JMenu jmMenuTest;
            JMenuItem jmiMTNew;
          JMenu jmSearch;
          JMenu jmInsert;
            JMenuItem jmiPatterns;
          JMenu jmFont;
            JMenuItem jmiSetFont;
          JMenu jmHelp;
            JMenuItem jmiHelpIntro;
          JMenu jmRun;

        JTextArea jta;
        
        // ==========================================================================
        // work data
        
        // ==========================================================================
        // constructors
        
      public MenuTest() {
          super();

          mtMe = this;
          
          setLayout( new BorderLayout() );
          
          theTitle = " Menu Test " ;

          jmb01 = new JMenuBar();
          setJMenuBar( jmb01 );
          
          jmMenuTest = new JMenu( theTitle );
          jmMenuTest.setMnemonic( 'M' );
          jmb01.add( jmMenuTest );
          
            jmiMTNew = new JMenuItem( " New " );
            jmiMTNew.setMnemonic( 'N' );
            jmMenuTest.add( jmiMTNew );
          
          jmSearch = new JMenu( " Search " );
          jmSearch.setMnemonic( 'S' );
          jmb01.add( jmSearch );
          
           jmInsert = new JMenu( " Insert " );
           jmInsert.setMnemonic( 'I' );
           jmb01.add( jmInsert );

             jmiPatterns = new JMenuItem( " Patterns " );
             jmiPatterns.setMnemonic( 'P' );
             jmInsert.add( jmiPatterns );

           jmFont = new JMenu( " Font " );
           jmFont.setMnemonic( 'F' );
           jmb01.add( jmFont );

             jmiSetFont = new JMenuItem( " Set Font " );
             jmiSetFont.setMnemonic( 'F' );
             jmFont.add( jmiSetFont );

          jmRun = new JMenu( " Run " );
          jmRun.setMnemonic( 'R' );
          jmb01.add( jmRun );

          jmHelp = new JMenu( " My Help " );
          jmHelp.setMnemonic( 'M' );
          jmb01.add( jmHelp );

            jmiHelpIntro = new JMenuItem( " Intro " );
            jmiHelpIntro.setMnemonic( 'I' );
            jmHelp.add( jmiHelpIntro );


          // connect MenuItems and event handling
          mil = new MenuItemListener( this );

          jmiMTNew.addActionListener( mil );
          jmiPatterns.addActionListener( mil );
          jmiSetFont.addActionListener( mil );
          jmiHelpIntro.addActionListener( mil );
          
          jta = new JTextArea(10, 40 );
          add( jta, BorderLayout.CENTER );

          
          setTitle( theTitle );
          
          pack();

          setSize( 400, 100 );
          setLocationRelativeTo( null );


          addWindowListener( new MTWdwListener() );

          // window closing
          setDefaultCloseOperation( EXIT_ON_CLOSE );

      }
        // ==========================================================================

        // --- main() ---------------------------------------------------------------
      public static void main(String[] args) {

      MenuTest mt = new MenuTest();
      mt.setVisible( true );

      } // --- end method main() --------------------------------------------------

        // ========================================================================
        class MTWdwListener extends WindowAdapter {
          public void windowClosing( WindowEvent we ) {
            System.exit( 0 ); // dispose();
          }
        } // ======================================================================

      } // === end class HTMLEditor =================================================

      // second File ################################################
      package menutest;

      import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
      import javax.swing.JMenuItem;
      import javax.swing.JOptionPane;

      public class MenuItemListener implements ActionListener {

      MenuTest mt;

      MenuItemListener( MenuTest mt ) {
      this.mt = mt;
      }

      public void actionPerformed( ActionEvent ae ) {

      JMenuItem jmiSrcOfae = ( JMenuItem )ae.getSource();

      if ( jmiSrcOfae == mt.jmiMTNew ) {
          JOptionPane.showMessageDialog( mt, " New not implemented " );
      return;
      }

      if ( jmiSrcOfae == mt.jmiPatterns ) {
          JOptionPane.showMessageDialog( mt, " Insert Patterns not implemented " );
      return;
      }

      if ( jmiSrcOfae == mt.jmiSetFont ) {
          JOptionPane.showMessageDialog( mt, " Set Font not implemented " );
      return;
      }

      if ( jmiSrcOfae == mt.jmiHelpIntro ) {
          JOptionPane.showMessageDialog( mt, " Help Intor not implemented " );
      return;
      }

      } // --- end method actionPreformed() ---------------------------------------

      } // === end class MenuItemListener ===========================================


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

      CUSTOMER SUBMITTED WORKAROUND :
      Choose another Mnemonic for the two JMenu objects

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: