Path: blob/master/test/jdk/javax/swing/JTextField/8036819/bug8036819.java
41153 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @library ../../regtesthelpers27* @build Util28* @bug 803681929* @summary JAB: mnemonics not read for textboxes30* @author Vivi An31* @run main bug803681932*/3334import javax.swing.*;35import javax.swing.event.*;36import java.awt.event.*;37import java.awt.*;38import javax.accessibility.*;3940public class bug8036819 {4142public static volatile Boolean passed = false;4344public static void main(String args[]) throws Throwable {45SwingUtilities.invokeAndWait(new Runnable() {46public void run() {47createAndShowGUI();48}49});505152Robot robo = new Robot();53robo.setAutoDelay(300);54robo.waitForIdle();5556// Using mnemonic key to focus on the textfield57Util.hitMnemonics(robo, KeyEvent.VK_P);58robo.waitForIdle();5960if (!passed){61throw new RuntimeException("Test failed.");62}63}6465private static void createAndShowGUI() {66JFrame mainFrame = new JFrame("bug 8036819");6768JLabel usernameLabel = new JLabel("Username: ");69JTextField usernameField = new JTextField(20);70usernameLabel.setDisplayedMnemonic(KeyEvent.VK_U);71usernameLabel.setLabelFor(usernameField);7273JLabel pwdLabel = new JLabel("Password: ");74JTextField pwdField = new JTextField(20);75pwdLabel.setDisplayedMnemonic(KeyEvent.VK_P);76pwdLabel.setLabelFor(pwdField);7778pwdField.addKeyListener(79new KeyListener(){80@Override81public void keyPressed(KeyEvent keyEvent) {82}8384@Override85public void keyTyped(KeyEvent keyEvent) {86}8788@Override89public void keyReleased(KeyEvent keyEvent){90JComponent comp = (JComponent) pwdField;91AccessibleContext ac = comp.getAccessibleContext();92AccessibleExtendedComponent aec = (AccessibleExtendedComponent)ac.getAccessibleComponent();93AccessibleKeyBinding akb = aec.getAccessibleKeyBinding();94if (akb != null){95int count = akb.getAccessibleKeyBindingCount();96if (count != 1){97passed = false;98return;99}100101// there is 1 accessible key for the text field102System.out.println("Retrieved AccessibleKeyBinding for textfield " + count);103104// the key code is KeyEvent.VK_P105Object o = akb.getAccessibleKeyBinding(0);106if (o instanceof KeyStroke){107javax.swing.KeyStroke key = (javax.swing.KeyStroke)o;108System.out.println("keystroke is " + key.getKeyCode());109if (key.getKeyCode() == KeyEvent.VK_P)110passed = true;111}112}113}114}115);116117mainFrame.getContentPane().add(usernameLabel);118mainFrame.getContentPane().add(usernameField);119mainFrame.getContentPane().add(pwdLabel);120mainFrame.getContentPane().add(pwdField);121122mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);123mainFrame.setLayout(new FlowLayout(FlowLayout.LEFT));124125mainFrame.setSize(200, 200);126mainFrame.setLocation(200, 200);127mainFrame.setVisible(true);128mainFrame.toFront();129}130}131132133