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 /* 25 * @test 26 * @key headful 27 * @library ../../regtesthelpers 28 * @build Util 29 * @bug 8036819 30 * @summary JAB: mnemonics not read for textboxes 31 * @author Vivi An 32 * @run main bug8036819 33 */ 34 35 import javax.swing.*; 36 import javax.swing.event.*; 37 import java.awt.event.*; 38 import java.awt.*; 39 import sun.awt.SunToolkit; 40 import javax.accessibility.*; 41 42 public class bug8036819 { 43 44 public static volatile Boolean passed = false; 45 46 public static void main(String args[]) throws Throwable { 47 SwingUtilities.invokeAndWait(new Runnable() { 48 public void run() { 49 createAndShowGUI(); 50 } 51 }); 52 53 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 54 toolkit.realSync(); 55 56 Robot robo = new Robot(); 57 robo.setAutoDelay(300); 58 59 // Using mnemonic key to focus on the textfield 60 Util.hitMnemonics(robo, KeyEvent.VK_P); 61 toolkit.realSync(); 62 63 if (!passed){ 64 throw new RuntimeException("Test failed."); 65 } 66 } 67 68 private static void createAndShowGUI() { 69 JFrame mainFrame = new JFrame("bug 8036819"); 70 71 JLabel usernameLabel = new JLabel("Username: "); 72 JTextField usernameField = new JTextField(20); 73 usernameLabel.setDisplayedMnemonic(KeyEvent.VK_U); 74 usernameLabel.setLabelFor(usernameField); 75 76 JLabel pwdLabel = new JLabel("Password: "); 77 JTextField pwdField = new JTextField(20); 78 pwdLabel.setDisplayedMnemonic(KeyEvent.VK_P); 79 pwdLabel.setLabelFor(pwdField); 80 81 pwdField.addKeyListener( 82 new KeyListener(){ 83 @Override 84 public void keyPressed(KeyEvent keyEvent) { 85 } 86 87 @Override 88 public void keyTyped(KeyEvent keyEvent) { 89 } 90 91 @Override 92 public void keyReleased(KeyEvent keyEvent){ 93 JComponent comp = (JComponent) pwdField; 94 AccessibleContext ac = comp.getAccessibleContext(); 95 AccessibleExtendedComponent aec = (AccessibleExtendedComponent)ac.getAccessibleComponent(); 96 AccessibleKeyBinding akb = aec.getAccessibleKeyBinding(); 97 if (akb != null){ 98 int count = akb.getAccessibleKeyBindingCount(); 99 if (count != 1){ 100 passed = false; 101 return; 102 } 103 104 // there is 1 accessible key for the text field 105 System.out.println("Retrieved AccessibleKeyBinding for textfield " + count); 106 107 // the key code is KeyEvent.VK_P 108 Object o = akb.getAccessibleKeyBinding(0); 109 if (o instanceof KeyStroke){ 110 javax.swing.KeyStroke key = (javax.swing.KeyStroke)o; 111 System.out.println("keystroke is " + key.getKeyCode()); 112 if (key.getKeyCode() == KeyEvent.VK_P) 113 passed = true; 114 } 115 } 116 } 117 } 118 ); 119 120 mainFrame.getContentPane().add(usernameLabel); 121 mainFrame.getContentPane().add(usernameField); 122 mainFrame.getContentPane().add(pwdLabel); 123 mainFrame.getContentPane().add(pwdField); 124 125 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 126 mainFrame.setLayout(new FlowLayout(FlowLayout.LEFT)); 127 128 mainFrame.setSize(200, 200); 129 mainFrame.setLocation(200, 200); 130 mainFrame.setVisible(true); 131 mainFrame.toFront(); 132 } 133 }