1 /* |
|
2 * Copyright (c) 2014, 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 import javax.swing.*; |
|
25 import java.awt.event.ActionEvent; |
|
26 import java.awt.event.KeyEvent; |
|
27 |
|
28 /* |
|
29 @test |
|
30 @bug 8031485 |
|
31 @summary Combo box consuming escape and enter key events |
|
32 @author Petr Pchelko |
|
33 @library ../../../../lib/testlibrary/ |
|
34 @build ExtendedRobot |
|
35 @run main ConsumedEscTest |
|
36 */ |
|
37 public class ConsumedEscTest { |
|
38 private static volatile JFrame frame; |
|
39 private static volatile boolean passed = false; |
|
40 |
|
41 public static void main(String... args) throws Exception { |
|
42 try { |
|
43 SwingUtilities.invokeAndWait(() -> { |
|
44 frame = new JFrame(); |
|
45 JComboBox<String> combo = new JComboBox<>(new String[]{"one", "two", "three"}); |
|
46 JPanel panel = new JPanel(); |
|
47 panel.add(combo); |
|
48 combo.requestFocusInWindow(); |
|
49 frame.setBounds(100, 150, 300, 100); |
|
50 addAction(panel); |
|
51 frame.add(panel); |
|
52 frame.setVisible(true); |
|
53 }); |
|
54 |
|
55 ExtendedRobot robot = new ExtendedRobot(); |
|
56 robot.waitForIdle(); |
|
57 robot.type(KeyEvent.VK_ESCAPE); |
|
58 robot.waitForIdle(); |
|
59 if (!passed) { |
|
60 throw new RuntimeException("FAILED: ESC was consumed by combo box"); |
|
61 } |
|
62 } finally { |
|
63 if (frame != null) { |
|
64 frame.dispose(); |
|
65 } |
|
66 } |
|
67 } |
|
68 |
|
69 private static void addAction(JComponent comp) { |
|
70 KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); |
|
71 Object actionKey = "cancel"; |
|
72 comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, actionKey); |
|
73 Action cancelAction = new AbstractAction() { |
|
74 @Override |
|
75 public void actionPerformed(ActionEvent ev) { |
|
76 passed = true; |
|
77 } |
|
78 }; |
|
79 comp.getActionMap().put(actionKey, cancelAction); |
|
80 } |
|
81 |
|
82 } |
|