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

MediumWeight JPopupMenus steal Focus, block mnemonics & accelerators

XMLWordPrintable



      Name: rk38400 Date: 04/08/98


      import java.awt.BorderLayout;
      import java.awt.Panel;
      import java.awt.event.*;
      import com.sun.java.swing.*;
       
      /** This class demonstrates that MEDUIM WEIGHT JPopupMenus
        * steal focus and don't handle mnemonics or accelerator keys.
        *
        * Forcing a MEDIUM WEIGHT popup menu requires the following steps.
        * Step 1: Run the application.
        * Step 2: Resize the main window so that Menu 1 obscures the JTextField
        * text, but fits completely within the main window.
        *
        * BUGS: Give focus to the JTextField and popup Menu 1 using the
        * mnemonic '1'. Note that the MenuItem that lands over the text
        * CURSOR is highlighted. (This focus jump is also seen in HeavyWeight
        * menus & on WinNT.)
        * At this point, you can't traverse the menu, use the MenuItem
        * mnemonics or any accelerators (Menu 1's or Menu 2's).
        * ALSO, using Menu 1 with the mouse does not return focus to the
        * JTextField, like (HeavyWeight) Menu 2 does.
        *
        *
        * Solsparc 2.6; JDK 1.1.5; Swing 1.0.1
        * ###@###.###
        */
      class Menu1 extends JFrame implements ActionListener
      {
              Menu1()
              {
                      // use MediumWeight instead of LightWeight
                      JPopupMenu.setDefaultLightWeightPopupEnabled(false);
       
                      JMenuBar mb = new JMenuBar();
                      setJMenuBar(mb);
       
                      JMenu menu = new JMenu("Menu 1");
                      mb.add(menu);
                      menu.setMnemonic('1');
                      for (int i = 3 ; i < 6 ; i++)
                      {
                              addMenuItem(menu, i);
                      }
       
                      menu = new JMenu("Menu 2");
                      mb.add(menu);
                      menu.setMnemonic('2');
                      for (int i = 6 ; i < 15 ; i++)
                      {
                              addMenuItem(menu, i);
                      }
       
                      getContentPane().add(new JTextField("takes up space"));
              }
       
              private void addMenuItem(JMenu menu, int i)
              {
                      JMenuItem item = new JMenuItem("MenuItem " + i);
                      menu.add(item);
       
                      item.addActionListener(this);
                      item.setMnemonic('0' + i);
                      item.setAccelerator(
                              KeyStroke.getKeyStroke((char)(KeyEvent.VK_A + i),
                                                              KeyEvent.ALT_MASK));
              }
       
              public void actionPerformed(ActionEvent e)
              {
                      System.out.println(e.getActionCommand());
              }
       
              public static void main(String args[])
              {
                      JFrame test = new Menu1();
                      test.pack();
                      test.show();
              }
      }
      (Review ID: 26737)
      ======================================================================

            gsaab Georges Saab
            rkarsunw Ralph Kar (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: