1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 @bug 8173145 26 @summary Menu is activated after using mnemonic Alt/Key combination 27 @modules java.desktop/com.sun.java.swing.plaf.windows 28 @run main Test8173145 29 */ 30 31 import java.awt.*; 32 import java.awt.event.KeyEvent; 33 import java.lang.reflect.InvocationTargetException; 34 35 import javax.swing.*; 36 37 public class Test8173145 { 38 39 private volatile static JButton btn; 40 private volatile static boolean uiCreated; 41 42 public static void main(String[] args) throws InvocationTargetException, InterruptedException, AWTException { 43 SwingUtilities.invokeAndWait(new Runnable() { 44 @Override 45 public void run() { 46 try { 47 uiCreated = createGUI(); 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } 51 } 52 }); 53 54 if (uiCreated) { 55 test(); 56 } else { 57 //no windows l&f, skip the test 58 } 59 } 60 61 private static void test() { 62 final Robot robot; 63 try { 64 robot = new Robot(); 65 } catch (AWTException e) { 66 throw new RuntimeException(e); 67 } 68 robot.setAutoDelay(100); 69 robot.waitForIdle(); 70 71 robot.keyPress(KeyEvent.VK_ALT); 72 robot.keyPress(KeyEvent.VK_M); 73 robot.keyRelease(KeyEvent.VK_M); 74 robot.keyRelease(KeyEvent.VK_ALT); 75 76 Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 77 78 if (focusOwner != btn) { 79 throw new RuntimeException("Wrong focus owner"); 80 } 81 } 82 83 private static boolean createGUI() { 84 try { 85 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 86 } catch (Exception e) { 87 return false; 88 } 89 JFrame f = new JFrame(); 90 91 JPanel panel = new JPanel(); 92 btn = new JButton("Mmmmm"); 93 btn.setMnemonic(KeyEvent.VK_M); 94 btn.setDisplayedMnemonicIndex(0); 95 panel.add(btn); 96 97 JTextField tf = new JTextField(); 98 tf.setColumns(10); 99 panel.add(tf); 100 101 f.setJMenuBar(getMenuBar()); 102 f.add(panel); 103 f.pack(); 104 f.setVisible(true); 105 tf.requestFocus(); 106 return true; 107 } 108 109 static JMenuBar getMenuBar() { 110 JMenuBar menuBar; 111 JMenu menu; 112 113 menuBar = new JMenuBar(); 114 115 menu = new JMenu("Menu"); 116 menuBar.add(menu); 117 118 JMenuItem mi = new JMenuItem("test"); 119 menu.add(mi); 120 121 return menuBar; 122 } 123 }