JDK-4127053 : MediumWeight JPopupMenus steal Focus, block mnemonics & accelerators
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.5
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.6
  • CPU: sparc
  • Submitted: 1998-04-08
  • Updated: 2000-11-16
  • Resolved: 2000-11-16
Related Reports
Duplicate :  
Description

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)
======================================================================

Comments
EVALUATION It might be possible to work around this by making it return isFocusTraversable false. Name: keR10081 Date: 11/16/2000 This bug is no longer reproducible after focus management enhancement putback and would be closed as a duplicate of 4290675. ###@###.### ======================================================================
11-06-2004