Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JMenu/4692443/bug4692443.java
38853 views
/*1* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @library ../../regtesthelpers26* @build Util27* @bug 4692443 710503028* @summary JMenu: MenuListener.menuSelected() event fired too late when using mnemonics29* @author Alexander Zuev30* @run main bug469244331*/3233import javax.swing.*;34import javax.swing.event.*;35import java.awt.event.*;36import java.awt.*;3738public class bug4692443 {3940public static PassedListener pass;41public static FailedListener fail;42public static volatile Boolean passed;4344public static void main(String args[]) throws Throwable {4546fail = new FailedListener();47pass = new PassedListener();48passed = false;49Robot robo = new Robot();5051SwingUtilities.invokeAndWait(new Runnable() {52public void run() {53createAndShowGUI();54}55});5657robo.waitForIdle();5859int altKey = java.awt.event.KeyEvent.VK_ALT;60robo.setAutoDelay(100);61Util.hitMnemonics(robo, KeyEvent.VK_F); // Enter File menu62robo.keyPress(KeyEvent.VK_S); // Enter submenu63robo.keyRelease(KeyEvent.VK_S);64robo.keyPress(KeyEvent.VK_O); // Launch "One" action65robo.keyRelease(KeyEvent.VK_O);66robo.keyPress(KeyEvent.VK_M); // Launch "One" action67robo.keyRelease(KeyEvent.VK_M);6869robo.waitForIdle();7071if (!passed) {72throw new RuntimeException("Test failed.");73}7475}7677private static void createAndShowGUI() {78JFrame mainFrame = new JFrame("Bug 4692443");79JMenuBar mbar = new JMenuBar();80JMenu menu = new JMenu("File");81menu.setMnemonic('F');82menu.add(new JMenuItem("Menu Item 1")).setMnemonic('I');83final JMenu submenu = new JMenu("Submenu");84submenu.setMnemonic('S');85submenu.addMenuListener(new MenuListener() {86public void menuSelected(MenuEvent e) {87JMenuItem item = submenu.add(new JMenuItem("One", 'O'));88item.addActionListener(pass);89submenu.add(new JMenuItem("Two", 'w'));90submenu.add(new JMenuItem("Three", 'r'));91}92public void menuDeselected(MenuEvent e) {93submenu.removeAll();94}95public void menuCanceled(MenuEvent e) {96submenu.removeAll();97}98});99menu.add(submenu);100JMenuItem menuItem = menu.add(new JMenuItem("Menu Item 2"));101menuItem.setMnemonic('M');102menuItem.addActionListener(fail);103mbar.add(menu);104mainFrame.setJMenuBar(mbar);105106mainFrame.setSize(200, 200);107mainFrame.setLocation(200, 200);108mainFrame.setVisible(true);109mainFrame.toFront();110}111112public static class FailedListener implements ActionListener {113public void actionPerformed(ActionEvent ev) {114throw new RuntimeException("Test failed.");115}116}117118public static class PassedListener implements ActionListener {119public void actionPerformed(ActionEvent ev) {120passed = true;121}122}123124}125126127