1 /* 2 * Copyright (c) 2015, 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 import java.awt.AWTException; 25 import java.awt.Robot; 26 import java.awt.event.ActionEvent; 27 import java.awt.event.ActionListener; 28 import java.awt.event.KeyEvent; 29 import java.lang.reflect.InvocationTargetException; 30 import javax.swing.JButton; 31 import javax.swing.JFrame; 32 import javax.swing.JMenu; 33 import javax.swing.JMenuBar; 34 import javax.swing.JMenuItem; 35 import javax.swing.JTextArea; 36 import javax.swing.SwingUtilities; 37 38 /* 39 * @test 40 * @key headful 41 * @bug 4213634 8017187 42 * @author Scott Violet 43 * @library ../../regtesthelpers 44 * @build Util 45 * @run main bug4213634 46 */ 47 48 49 public class bug4213634 { 50 51 private JMenu menu; 52 53 private JFrame frame; 54 55 public static void main(String[] args) throws Throwable { 56 new bug4213634(); 57 } 58 59 bug4213634() throws AWTException, InterruptedException, InvocationTargetException { 60 SwingUtilities.invokeAndWait(new Runnable() { 61 @Override 62 public void run() { 63 createAndShowGUI(); 64 } 65 }); 66 67 test(); 68 } 69 70 public void createAndShowGUI() { 71 frame = new JFrame("TEST"); 72 JMenuBar mb = new JMenuBar(); 73 menu = mb.add(createMenu("1 - First Menu", true)); 74 mb.add(createMenu("2 - Second Menu", false)); 75 frame.setJMenuBar(mb); 76 JTextArea ta = new JTextArea("This test dedicated to Nancy and Kathleen, testers and bowlers extraordinaire\n\n\nNo exception means pass."); 77 frame.getContentPane().add("Center", ta); 78 JButton button = new JButton("Test"); 79 frame.getContentPane().add("South", button); 80 frame.setBounds(100, 100, 400, 400); 81 frame.setVisible(true); 82 button.requestFocusInWindow(); 83 } 84 85 private void test() throws AWTException, InterruptedException, InvocationTargetException { 86 Robot robot = new Robot(); 87 robot.setAutoDelay(50); 88 robot.waitForIdle(); 89 90 Util.hitMnemonics(robot, KeyEvent.VK_1); 91 robot.waitForIdle(); 92 93 SwingUtilities.invokeAndWait(new Runnable() { 94 @Override 95 public void run() { 96 if (!menu.isSelected()) { 97 throw new RuntimeException( 98 "Failed: Menu didn't remain posted at end of test"); 99 } else { 100 System.out.println("Test passed!"); 101 frame.dispose(); 102 } 103 } 104 }); 105 } 106 private JMenu createMenu(String str, boolean bFlag) { 107 JMenuItem menuitem; 108 JMenu menu = new JMenu(str); 109 menu.setMnemonic(str.charAt(0)); 110 111 for(int i = 0; i < 10; i ++) { 112 menuitem = new JMenuItem("JMenuItem" + i); 113 menuitem.addActionListener(new ActionListener() { 114 public void actionPerformed(ActionEvent e) { 115 throw new RuntimeException( 116 "Failed: Mnemonic activated"); 117 } 118 }); 119 if(bFlag) 120 menuitem.setMnemonic('0' + i); 121 menu.add(menuitem); 122 } 123 return menu; 124 } 125 }