Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JMenu/4692443/bug4692443.java
41153 views
1
/*
2
* Copyright (c) 2009, 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
* @library ../../regtesthelpers
28
* @build Util
29
* @bug 4692443 7105030
30
* @summary JMenu: MenuListener.menuSelected() event fired too late when using mnemonics
31
* @author Alexander Zuev
32
* @run main bug4692443
33
*/
34
35
import javax.swing.*;
36
import javax.swing.event.*;
37
import java.awt.event.*;
38
import java.awt.*;
39
40
public class bug4692443 {
41
42
public static PassedListener pass;
43
public static FailedListener fail;
44
public static volatile Boolean passed;
45
public static JFrame mainFrame;
46
47
public static void main(String args[]) throws Throwable {
48
try {
49
fail = new FailedListener();
50
pass = new PassedListener();
51
passed = false;
52
Robot robo = new Robot();
53
robo.setAutoDelay(100);
54
55
SwingUtilities.invokeAndWait(new Runnable() {
56
public void run() {
57
createAndShowGUI();
58
}
59
});
60
61
robo.waitForIdle();
62
robo.delay(1000);
63
64
int altKey = java.awt.event.KeyEvent.VK_ALT;
65
Util.hitMnemonics(robo, KeyEvent.VK_F); // Enter File menu
66
robo.keyPress(KeyEvent.VK_S); // Enter submenu
67
robo.keyRelease(KeyEvent.VK_S);
68
robo.keyPress(KeyEvent.VK_O); // Launch "One" action
69
robo.keyRelease(KeyEvent.VK_O);
70
robo.keyPress(KeyEvent.VK_M); // Launch "One" action
71
robo.keyRelease(KeyEvent.VK_M);
72
73
robo.waitForIdle();
74
75
if (!passed) {
76
throw new RuntimeException("Test failed.");
77
}
78
} finally {
79
if (mainFrame != null) SwingUtilities.invokeAndWait(() -> mainFrame.dispose());
80
}
81
}
82
83
84
private static void createAndShowGUI() {
85
mainFrame = new JFrame("Bug 4692443");
86
JMenuBar mbar = new JMenuBar();
87
JMenu menu = new JMenu("File");
88
menu.setMnemonic('F');
89
menu.add(new JMenuItem("Menu Item 1")).setMnemonic('I');
90
final JMenu submenu = new JMenu("Submenu");
91
submenu.setMnemonic('S');
92
submenu.addMenuListener(new MenuListener() {
93
public void menuSelected(MenuEvent e) {
94
JMenuItem item = submenu.add(new JMenuItem("One", 'O'));
95
item.addActionListener(pass);
96
submenu.add(new JMenuItem("Two", 'w'));
97
submenu.add(new JMenuItem("Three", 'r'));
98
}
99
public void menuDeselected(MenuEvent e) {
100
submenu.removeAll();
101
}
102
public void menuCanceled(MenuEvent e) {
103
submenu.removeAll();
104
}
105
});
106
menu.add(submenu);
107
JMenuItem menuItem = menu.add(new JMenuItem("Menu Item 2"));
108
menuItem.setMnemonic('M');
109
menuItem.addActionListener(fail);
110
mbar.add(menu);
111
mainFrame.setJMenuBar(mbar);
112
113
mainFrame.setSize(200, 200);
114
mainFrame.setLocationRelativeTo(null);
115
mainFrame.setVisible(true);
116
mainFrame.toFront();
117
}
118
119
public static class FailedListener implements ActionListener {
120
public void actionPerformed(ActionEvent ev) {
121
throw new RuntimeException("Test failed.");
122
}
123
}
124
125
public static class PassedListener implements ActionListener {
126
public void actionPerformed(ActionEvent ev) {
127
passed = true;
128
}
129
}
130
131
}
132
133