1 /* 2 * Copyright (c) 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 * @bug 4458079 28 * @library ../../regtesthelpers 29 * @build Util 30 * @summary Tests calling removeAll() from PopupMenuListener 31 * @author Peter Zhelezniakov 32 * @run main bug4458079 33 */ 34 35 import java.awt.Robot; 36 import java.awt.Toolkit; 37 import java.awt.event.*; 38 import javax.swing.*; 39 import javax.swing.event.*; 40 import java.awt.event.KeyEvent; 41 import java.util.ArrayList; 42 import sun.awt.SunToolkit; 43 44 public class bug4458079 extends JFrame implements PopupMenuListener { 45 public JMenu menu; 46 47 static volatile boolean itemASelected = false; 48 public static void main(String[] args) throws Exception { 49 SwingUtilities.invokeAndWait(new Runnable() { 50 public void run() { 51 new bug4458079().createAndShowGUI(); 52 } 53 }); 54 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 55 toolkit.realSync(); 56 57 Robot robot = new Robot(); 58 robot.setAutoDelay(50); 59 60 Util.hitMnemonics(robot, KeyEvent.VK_M); 61 62 toolkit.realSync(); 63 Thread.sleep(1000); 64 65 Util.hitKeys(robot, KeyEvent.VK_DOWN); 66 Util.hitKeys(robot, KeyEvent.VK_ENTER); 67 68 toolkit.realSync(); 69 Thread.sleep(1000); 70 71 if (!itemASelected) { 72 throw new RuntimeException("Test failed: arrow key traversal in JMenu broken!"); 73 } 74 } 75 public void createAndShowGUI() { 76 JMenuBar bar = new JMenuBar(); 77 menu = new JMenu("Menu"); 78 menu.add(new JMenuItem("1")); 79 menu.add(new JMenuItem("2")); 80 menu.setMnemonic(KeyEvent.VK_M); 81 menu.getPopupMenu().addPopupMenuListener(this); 82 bar.add(menu); 83 84 setJMenuBar(bar); 85 getContentPane().add(new JButton("")); 86 setSize(300, 300); 87 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 88 pack(); 89 setVisible(true); 90 } 91 92 public void rebuildMenu() { 93 menu.removeAll(); 94 final String itemCommand = "A"; 95 JMenuItem item = new JMenuItem(itemCommand); 96 item.addActionListener(new ActionListener() { 97 public void actionPerformed(ActionEvent e) { 98 JMenuItem item = ((JMenuItem)e.getSource()); 99 if (e.getActionCommand() == itemCommand) { 100 itemASelected = true; 101 } 102 } 103 }); 104 menu.add(item); 105 menu.add(new JMenuItem("B")); 106 } 107 108 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 109 rebuildMenu(); 110 } 111 112 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {} 113 public void popupMenuCanceled(PopupMenuEvent e) {} 114 }