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