1 /* 2 * Copyright (c) 2017, 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 8189253 26 * @key headful 27 * @summary JPopupMenu is inadvertently shown when using setComponentPopupMenu 28 * @compile LightWeightComponentPopupMenu.java 29 * @run main/manual LightWeightComponentPopupMenu 30 */ 31 import javax.swing.JDialog; 32 import javax.swing.JFrame; 33 import javax.swing.JMenuItem; 34 import javax.swing.JPopupMenu; 35 import javax.swing.JTextArea; 36 import javax.swing.JButton; 37 import java.awt.FlowLayout; 38 import java.awt.event.ActionEvent; 39 import java.awt.event.ActionListener; 40 41 public class LightWeightComponentPopupMenu { 42 private static JButton passButton; 43 private static JButton failButton; 44 private static JTextArea instructions; 45 private static JFrame mainFrame; 46 private static JFrame instructionFrame; 47 public static boolean isProgInterruption = false; 48 static Thread mainThread = null; 49 static int sleepTime = 300000; 50 51 public LightWeightComponentPopupMenu() { 52 passButton = new JButton("Pass"); 53 passButton.setEnabled(true); 54 55 failButton = new JButton("Fail"); 56 failButton.setEnabled(true); 57 58 instructions = new JTextArea(8, 30); 59 instructions.setText(" This is a manual test\n\n" + 60 " 1) Right click on the window titled Main, a popup menu should be displayed\n" + 61 " 2) Move mouse to the dialog titled Modeless & right click on it\n" + 62 " 3) Press Pass if popup menu is not displayed when clicked on the Modeless dialog,\n" + 63 " 4) Press Fail otherwise"); 64 65 instructionFrame = new JFrame("Test Instructions"); 66 instructionFrame.add(passButton); 67 instructionFrame.add(failButton); 68 instructionFrame.add(instructions); 69 instructionFrame.setSize(200,200); 70 instructionFrame.setLayout(new FlowLayout()); 71 instructionFrame.pack(); 72 instructionFrame.setVisible(true); 73 74 passButton.addActionListener(new ActionListener() { 75 @Override 76 public void actionPerformed(ActionEvent ae) { 77 dispose(); 78 isProgInterruption = true; 79 mainThread.interrupt(); 80 } 81 }); 82 83 failButton.addActionListener(new ActionListener() { 84 @Override 85 public void actionPerformed(ActionEvent ae) { 86 dispose(); 87 isProgInterruption = true; 88 mainThread.interrupt(); 89 throw new RuntimeException("Popup menu is shown on the modeless dialog!"); 90 } 91 }); 92 } 93 94 private static void dispose() { 95 mainFrame.dispose(); 96 instructionFrame.dispose(); 97 } 98 private static void createAndShowPopupMenu() { 99 JPopupMenu popupMenu = new JPopupMenu(); 100 popupMenu.add(new JMenuItem("Popup menu")); 101 102 mainFrame = new JFrame("Main"); 103 mainFrame.getRootPane().setComponentPopupMenu(popupMenu); 104 mainFrame.setSize(400, 400); 105 mainFrame.setLocationRelativeTo(null); 106 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 107 mainFrame.setVisible(true); 108 109 JTextArea info = new JTextArea(""); 110 info.setWrapStyleWord(true); 111 info.setLineWrap(true); 112 113 JDialog dialog = new JDialog(mainFrame, "Modeless", false); 114 dialog.getContentPane().add(info); 115 dialog.setSize(300, 300); 116 dialog.setLocationRelativeTo(mainFrame); 117 dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 118 dialog.setVisible(true); 119 120 mainFrame.toFront(); 121 } 122 123 public static void main(String args[]) throws Exception { 124 mainThread = Thread.currentThread(); 125 LightWeightComponentPopupMenu test = new LightWeightComponentPopupMenu(); 126 test.createAndShowPopupMenu(); 127 128 try { 129 mainThread.sleep(sleepTime); 130 } catch (InterruptedException e) { 131 if (!isProgInterruption) { 132 throw e; 133 } 134 } finally { 135 test.dispose(); 136 } 137 138 if (!isProgInterruption) { 139 throw new RuntimeException("Timed out after " + sleepTime / 1000 140 + " seconds"); 141 } 142 } 143 } 144