Path: blob/master/test/jdk/javax/swing/JMenu/4692443/bug4692443.java
41153 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* @key headful26* @library ../../regtesthelpers27* @build Util28* @bug 4692443 710503029* @summary JMenu: MenuListener.menuSelected() event fired too late when using mnemonics30* @author Alexander Zuev31* @run main bug469244332*/3334import javax.swing.*;35import javax.swing.event.*;36import java.awt.event.*;37import java.awt.*;3839public class bug4692443 {4041public static PassedListener pass;42public static FailedListener fail;43public static volatile Boolean passed;44public static JFrame mainFrame;4546public static void main(String args[]) throws Throwable {47try {48fail = new FailedListener();49pass = new PassedListener();50passed = false;51Robot robo = new Robot();52robo.setAutoDelay(100);5354SwingUtilities.invokeAndWait(new Runnable() {55public void run() {56createAndShowGUI();57}58});5960robo.waitForIdle();61robo.delay(1000);6263int altKey = java.awt.event.KeyEvent.VK_ALT;64Util.hitMnemonics(robo, KeyEvent.VK_F); // Enter File menu65robo.keyPress(KeyEvent.VK_S); // Enter submenu66robo.keyRelease(KeyEvent.VK_S);67robo.keyPress(KeyEvent.VK_O); // Launch "One" action68robo.keyRelease(KeyEvent.VK_O);69robo.keyPress(KeyEvent.VK_M); // Launch "One" action70robo.keyRelease(KeyEvent.VK_M);7172robo.waitForIdle();7374if (!passed) {75throw new RuntimeException("Test failed.");76}77} finally {78if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());79}80}818283private static void createAndShowGUI() {84mainFrame = new JFrame("Bug 4692443");85JMenuBar mbar = new JMenuBar();86JMenu menu = new JMenu("File");87menu.setMnemonic('F');88menu.add(new JMenuItem("Menu Item 1")).setMnemonic('I');89final JMenu submenu = new JMenu("Submenu");90submenu.setMnemonic('S');91submenu.addMenuListener(new MenuListener() {92public void menuSelected(MenuEvent e) {93JMenuItem item = submenu.add(new JMenuItem("One", 'O'));94item.addActionListener(pass);95submenu.add(new JMenuItem("Two", 'w'));96submenu.add(new JMenuItem("Three", 'r'));97}98public void menuDeselected(MenuEvent e) {99submenu.removeAll();100}101public void menuCanceled(MenuEvent e) {102submenu.removeAll();103}104});105menu.add(submenu);106JMenuItem menuItem = menu.add(new JMenuItem("Menu Item 2"));107menuItem.setMnemonic('M');108menuItem.addActionListener(fail);109mbar.add(menu);110mainFrame.setJMenuBar(mbar);111112mainFrame.setSize(200, 200);113mainFrame.setLocationRelativeTo(null);114mainFrame.setVisible(true);115mainFrame.toFront();116}117118public static class FailedListener implements ActionListener {119public void actionPerformed(ActionEvent ev) {120throw new RuntimeException("Test failed.");121}122}123124public static class PassedListener implements ActionListener {125public void actionPerformed(ActionEvent ev) {126passed = true;127}128}129130}131132133