1 /* 2 * Copyright (c) 2009, 2013, 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 /* 25 * @test 26 * @key headful 27 * @library ../../regtesthelpers 28 * @build Util 29 * @bug 4692443 7105030 30 * @summary JMenu: MenuListener.menuSelected() event fired too late when using mnemonics 31 * @author Alexander Zuev 32 * @run main bug4692443 33 */ 34 35 import javax.swing.*; 36 import javax.swing.event.*; 37 import java.awt.event.*; 38 import java.awt.*; 39 import sun.awt.SunToolkit; 40 41 public class bug4692443 { 42 43 public static PassedListener pass; 44 public static FailedListener fail; 45 public static volatile Boolean passed; 46 47 public static void main(String args[]) throws Throwable { 48 49 fail = new FailedListener(); 50 pass = new PassedListener(); 51 passed = false; 52 Robot robo = new Robot(); 53 54 SwingUtilities.invokeAndWait(new Runnable() { 55 public void run() { 56 createAndShowGUI(); 57 } 58 }); 59 60 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 61 toolkit.realSync(); 62 63 try { 64 robo = new Robot(); 65 } catch (AWTException e) { 66 throw new RuntimeException("Robot could not be created"); 67 } 68 int altKey = java.awt.event.KeyEvent.VK_ALT; 69 robo.setAutoDelay(100); 70 Util.hitMnemonics(robo, KeyEvent.VK_F); // Enter File menu 71 robo.keyPress(KeyEvent.VK_S); // Enter submenu 72 robo.keyRelease(KeyEvent.VK_S); 73 robo.keyPress(KeyEvent.VK_O); // Launch "One" action 74 robo.keyRelease(KeyEvent.VK_O); 75 robo.keyPress(KeyEvent.VK_M); // Launch "One" action 76 robo.keyRelease(KeyEvent.VK_M); 77 78 toolkit.realSync(); 79 80 if (!passed) { 81 throw new RuntimeException("Test failed."); 82 } 83 84 } 85 86 private static void createAndShowGUI() { 87 JFrame mainFrame = new JFrame("Bug 4692443"); 88 JMenuBar mbar = new JMenuBar(); 89 JMenu menu = new JMenu("File"); 90 menu.setMnemonic('F'); 91 menu.add(new JMenuItem("Menu Item 1")).setMnemonic('I'); 92 final JMenu submenu = new JMenu("Submenu"); 93 submenu.setMnemonic('S'); 94 submenu.addMenuListener(new MenuListener() { 95 public void menuSelected(MenuEvent e) { 96 JMenuItem item = submenu.add(new JMenuItem("One", 'O')); 97 item.addActionListener(pass); 98 submenu.add(new JMenuItem("Two", 'w')); 99 submenu.add(new JMenuItem("Three", 'r')); 100 } 101 public void menuDeselected(MenuEvent e) { 102 submenu.removeAll(); 103 } 104 public void menuCanceled(MenuEvent e) { 105 submenu.removeAll(); 106 } 107 }); 108 menu.add(submenu); 109 JMenuItem menuItem = menu.add(new JMenuItem("Menu Item 2")); 110 menuItem.setMnemonic('M'); 111 menuItem.addActionListener(fail); 112 mbar.add(menu); 113 mainFrame.setJMenuBar(mbar); 114 115 mainFrame.setSize(200, 200); 116 mainFrame.setLocation(200, 200); 117 mainFrame.setVisible(true); 118 mainFrame.toFront(); 119 } 120 121 public static class FailedListener implements ActionListener { 122 public void actionPerformed(ActionEvent ev) { 123 throw new RuntimeException("Test failed."); 124 } 125 } 126 127 public static class PassedListener implements ActionListener { 128 public void actionPerformed(ActionEvent ev) { 129 passed = true; 130 } 131 } 132 133 }