Path: blob/master/test/jdk/javax/swing/JPopupMenu/4458079/bug4458079.java
41155 views
/*1* Copyright (c) 2013, 2017, 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* @bug 445807927* @library ../../regtesthelpers28* @build Util29* @summary Tests calling removeAll() from PopupMenuListener30* @author Peter Zhelezniakov31* @run main bug445807932*/3334import java.awt.Robot;35import java.awt.event.*;36import javax.swing.*;37import javax.swing.event.*;38import java.awt.event.KeyEvent;3940public class bug4458079 extends JFrame implements PopupMenuListener {41public JMenu menu;4243static volatile boolean itemASelected = false;44public static void main(String[] args) throws Exception {45Robot robot = new Robot();46robot.setAutoDelay(100);47// move mouse outside menu to prevent auto selection48robot.mouseMove(100,100);49robot.waitForIdle();5051SwingUtilities.invokeAndWait(new Runnable() {52public void run() {53new bug4458079().createAndShowGUI();54}55});5657robot.waitForIdle();58robot.delay(1000);59Util.hitMnemonics(robot, KeyEvent.VK_M);6061robot.waitForIdle();6263Util.hitKeys(robot, KeyEvent.VK_DOWN);64Util.hitKeys(robot, KeyEvent.VK_ENTER);6566robot.waitForIdle();6768if (!itemASelected) {69throw new RuntimeException("Test failed: arrow key traversal in JMenu broken!");70}71}72public void createAndShowGUI() {73JMenuBar bar = new JMenuBar();74menu = new JMenu("Menu");75menu.add(new JMenuItem("1"));76menu.add(new JMenuItem("2"));77menu.setMnemonic(KeyEvent.VK_M);78menu.getPopupMenu().addPopupMenuListener(this);79bar.add(menu);8081setJMenuBar(bar);82getContentPane().add(new JButton(""));83setSize(300, 300);84setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);85pack();86setLocationRelativeTo(null);87setVisible(true);88}8990public void rebuildMenu() {91menu.removeAll();92final String itemCommand = "A";93JMenuItem item = new JMenuItem(itemCommand);94item.addActionListener(new ActionListener() {95public void actionPerformed(ActionEvent e) {96JMenuItem item = ((JMenuItem)e.getSource());97if (e.getActionCommand() == itemCommand) {98itemASelected = true;99}100}101});102menu.add(item);103menu.add(new JMenuItem("B"));104}105106public void popupMenuWillBecomeVisible(PopupMenuEvent e) {107rebuildMenu();108}109110public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}111public void popupMenuCanceled(PopupMenuEvent e) {}112}113114115