|
1 | 1 | /* |
2 | | - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
21 | 21 | * questions. |
22 | 22 | */ |
23 | 23 |
|
24 | | -import java.awt.AWTException; |
25 | 24 | import java.awt.Robot; |
26 | | -import java.awt.event.ActionEvent; |
27 | | -import java.awt.event.ActionListener; |
28 | 25 | import java.awt.event.KeyEvent; |
29 | | -import java.lang.reflect.InvocationTargetException; |
30 | 26 | import javax.swing.JButton; |
31 | 27 | import javax.swing.JFrame; |
32 | 28 | import javax.swing.JMenu; |
33 | 29 | import javax.swing.JMenuBar; |
34 | 30 | import javax.swing.JMenuItem; |
35 | | -import javax.swing.JTextArea; |
36 | 31 | import javax.swing.SwingUtilities; |
37 | 32 |
|
38 | 33 | /* |
39 | 34 | * @test |
40 | 35 | * @key headful |
41 | 36 | * @bug 4213634 8017187 |
42 | | - * @author Scott Violet |
43 | 37 | * @library ../../regtesthelpers |
44 | 38 | * @build Util |
| 39 | + * @summary Verifies if Alt+mnemonic char works when |
| 40 | + * menu & menuitem have same mnemonic char |
45 | 41 | * @run main bug4213634 |
46 | 42 | */ |
47 | 43 |
|
48 | | - |
49 | 44 | public class bug4213634 { |
50 | 45 |
|
51 | | - private JMenu menu; |
52 | | - |
53 | | - private JFrame frame; |
| 46 | + private static JMenu menu; |
| 47 | + private static JFrame frame; |
| 48 | + private static Robot robot; |
54 | 49 |
|
55 | | - public static void main(String[] args) throws Throwable { |
56 | | - new bug4213634(); |
57 | | - } |
| 50 | + public static void main(String[] args) throws Exception { |
| 51 | + try { |
| 52 | + robot = new Robot(); |
| 53 | + SwingUtilities.invokeAndWait(bug4213634::createAndShowGUI); |
58 | 54 |
|
59 | | - bug4213634() throws AWTException, InterruptedException, InvocationTargetException { |
60 | | - SwingUtilities.invokeAndWait(new Runnable() { |
61 | | - @Override |
62 | | - public void run() { |
63 | | - createAndShowGUI(); |
64 | | - } |
65 | | - }); |
66 | | - |
67 | | - test(); |
| 55 | + robot.waitForIdle(); |
| 56 | + robot.delay(1000); |
| 57 | + test(); |
| 58 | + } finally { |
| 59 | + SwingUtilities.invokeAndWait(() -> { |
| 60 | + if (frame != null) { |
| 61 | + frame.dispose(); |
| 62 | + } |
| 63 | + }); |
| 64 | + } |
68 | 65 | } |
69 | 66 |
|
70 | | - public void createAndShowGUI() { |
71 | | - frame = new JFrame("TEST"); |
| 67 | + public static void createAndShowGUI() { |
| 68 | + frame = new JFrame("bug4213634"); |
72 | 69 | JMenuBar mb = new JMenuBar(); |
73 | 70 | menu = mb.add(createMenu("1 - First Menu", true)); |
74 | 71 | mb.add(createMenu("2 - Second Menu", false)); |
75 | 72 | frame.setJMenuBar(mb); |
76 | | - JTextArea ta = new JTextArea("This test dedicated to Nancy and Kathleen, testers and bowlers extraordinaire\n\n\nNo exception means pass."); |
77 | | - frame.getContentPane().add("Center", ta); |
78 | 73 | JButton button = new JButton("Test"); |
79 | 74 | frame.getContentPane().add("South", button); |
80 | | - frame.setBounds(100, 100, 400, 400); |
| 75 | + frame.setSize(300, 300); |
| 76 | + frame.setLocationRelativeTo(null); |
81 | 77 | frame.setVisible(true); |
82 | 78 | button.requestFocusInWindow(); |
83 | 79 | } |
84 | 80 |
|
85 | | - private void test() throws AWTException, InterruptedException, InvocationTargetException { |
86 | | - Robot robot = new Robot(); |
87 | | - robot.setAutoDelay(50); |
88 | | - robot.waitForIdle(); |
89 | | - |
| 81 | + private static void test() throws Exception { |
90 | 82 | Util.hitMnemonics(robot, KeyEvent.VK_1); |
91 | 83 | robot.waitForIdle(); |
| 84 | + robot.delay(100); |
92 | 85 |
|
93 | | - SwingUtilities.invokeAndWait(new Runnable() { |
94 | | - @Override |
95 | | - public void run() { |
96 | | - if (!menu.isSelected()) { |
97 | | - throw new RuntimeException( |
98 | | - "Failed: Menu didn't remain posted at end of test"); |
99 | | - } else { |
100 | | - System.out.println("Test passed!"); |
101 | | - frame.dispose(); |
102 | | - } |
| 86 | + SwingUtilities.invokeAndWait(() -> { |
| 87 | + if (!menu.isSelected()) { |
| 88 | + throw new RuntimeException( |
| 89 | + "Failed: Menu didn't remain posted at end of test"); |
| 90 | + } else { |
| 91 | + System.out.println("Test passed!"); |
103 | 92 | } |
104 | 93 | }); |
105 | 94 | } |
106 | | - private JMenu createMenu(String str, boolean bFlag) { |
| 95 | + |
| 96 | + private static JMenu createMenu(String str, boolean bFlag) { |
107 | 97 | JMenuItem menuitem; |
108 | 98 | JMenu menu = new JMenu(str); |
109 | 99 | menu.setMnemonic(str.charAt(0)); |
110 | 100 |
|
111 | | - for(int i = 0; i < 10; i ++) { |
| 101 | + for (int i = 0; i < 10; i++) { |
112 | 102 | menuitem = new JMenuItem("JMenuItem" + i); |
113 | | - menuitem.addActionListener(new ActionListener() { |
114 | | - public void actionPerformed(ActionEvent e) { |
115 | | - throw new RuntimeException( |
116 | | - "Failed: Mnemonic activated"); |
117 | | - } |
| 103 | + menuitem.addActionListener(e -> { |
| 104 | + throw new RuntimeException("Failed: Mnemonic activated"); |
118 | 105 | }); |
119 | | - if(bFlag) |
| 106 | + if (bFlag) { |
120 | 107 | menuitem.setMnemonic('0' + i); |
| 108 | + } |
121 | 109 | menu.add(menuitem); |
122 | 110 | } |
123 | 111 | return menu; |
|
0 commit comments