Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/4458079/bug4458079.java
38854 views
/*1* Copyright (c) 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*/22/* @test23@bug 445807924@library ../../regtesthelpers25@build Util26@summary Tests calling removeAll() from PopupMenuListener27@author Peter Zhelezniakov28@run main bug445807929*/30import java.awt.Robot;31import java.awt.Toolkit;32import java.awt.event.*;33import javax.swing.*;34import javax.swing.event.*;35import java.awt.event.KeyEvent;36import java.util.ArrayList;3738public class bug4458079 extends JFrame implements PopupMenuListener {39public JMenu menu;4041static volatile boolean itemASelected = false;42public static void main(String[] args) throws Exception {43SwingUtilities.invokeAndWait(new Runnable() {44public void run() {45new bug4458079().createAndShowGUI();46}47});48Robot robot = new Robot();49robot.waitForIdle();5051robot.setAutoDelay(50);5253Util.hitMnemonics(robot, KeyEvent.VK_M);5455robot.waitForIdle();56Thread.sleep(1000);5758Util.hitKeys(robot, KeyEvent.VK_DOWN);59Util.hitKeys(robot, KeyEvent.VK_ENTER);6061robot.waitForIdle();62Thread.sleep(1000);6364if (!itemASelected) {65throw new RuntimeException("Test failed: arrow key traversal in JMenu broken!");66}67}68public void createAndShowGUI() {69JMenuBar bar = new JMenuBar();70menu = new JMenu("Menu");71menu.add(new JMenuItem("1"));72menu.add(new JMenuItem("2"));73menu.setMnemonic(KeyEvent.VK_M);74menu.getPopupMenu().addPopupMenuListener(this);75bar.add(menu);7677setJMenuBar(bar);78getContentPane().add(new JButton(""));79setSize(300, 300);80setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);81pack();82setVisible(true);83}8485public void rebuildMenu() {86menu.removeAll();87final String itemCommand = "A";88JMenuItem item = new JMenuItem(itemCommand);89item.addActionListener(new ActionListener() {90public void actionPerformed(ActionEvent e) {91JMenuItem item = ((JMenuItem)e.getSource());92if (e.getActionCommand() == itemCommand) {93itemASelected = true;94}95}96});97menu.add(item);98menu.add(new JMenuItem("B"));99}100101public void popupMenuWillBecomeVisible(PopupMenuEvent e) {102rebuildMenu();103}104105public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {}106public void popupMenuCanceled(PopupMenuEvent e) {}107}108109110