1 /* 2 * Copyright (c) 2011, 2013, 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 * @bug 4624207 27 * @summary JTabbedPane mnemonics don't work from outside the tabbed pane 28 * @author Oleg Mokhovikov 29 * @library ../../regtesthelpers 30 * @build Util 31 * @run main bug4624207 32 */ 33 import javax.swing.*; 34 import javax.swing.event.ChangeEvent; 35 import javax.swing.event.ChangeListener; 36 import java.awt.*; 37 import java.awt.event.FocusEvent; 38 import java.awt.event.FocusListener; 39 import java.awt.event.KeyEvent; 40 41 import sun.awt.OSInfo; 42 import sun.awt.SunToolkit; 43 44 public class bug4624207 implements ChangeListener, FocusListener { 45 46 private static volatile boolean stateChanged = false; 47 private static volatile boolean focusGained = false; 48 private static JTextField txtField; 49 private static JTabbedPane tab; 50 private static Object listener; 51 52 public void stateChanged(ChangeEvent e) { 53 System.out.println("stateChanged called"); 54 stateChanged = true; 55 } 56 57 public void focusGained(FocusEvent e) { 58 System.out.println("focusGained called"); 59 focusGained = true; 60 } 61 62 public void focusLost(FocusEvent e) { 63 System.out.println("focusLost called"); 64 focusGained = false; 65 } 66 67 public static void main(String[] args) throws Exception { 68 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 69 Robot robot = new Robot(); 70 robot.setAutoDelay(50); 71 72 SwingUtilities.invokeAndWait(new Runnable() { 73 74 public void run() { 75 createAndShowGUI(); 76 } 77 }); 78 79 toolkit.realSync(); 80 81 SwingUtilities.invokeAndWait(new Runnable() { 82 83 public void run() { 84 txtField.requestFocus(); 85 } 86 }); 87 88 toolkit.realSync(); 89 90 if (!focusGained) { 91 throw new RuntimeException("Couldn't gain focus for text field"); 92 } 93 94 SwingUtilities.invokeAndWait(new Runnable() { 95 96 public void run() { 97 tab.addChangeListener((ChangeListener) listener); 98 txtField.removeFocusListener((FocusListener) listener); 99 } 100 }); 101 102 toolkit.realSync(); 103 104 if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) { 105 Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_B); 106 } else { 107 Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_B); 108 } 109 110 toolkit.realSync(); 111 112 if (!stateChanged || tab.getSelectedIndex() != 1) { 113 throw new RuntimeException("JTabbedPane mnemonics don't work from outside the tabbed pane"); 114 } 115 } 116 117 private static void createAndShowGUI() { 118 tab = new JTabbedPane(); 119 tab.add("Tab1", new JButton("Button1")); 120 tab.add("Tab2", new JButton("Button2")); 121 tab.setMnemonicAt(0, KeyEvent.VK_T); 122 tab.setMnemonicAt(1, KeyEvent.VK_B); 123 124 JFrame frame = new JFrame(); 125 frame.getContentPane().add(tab, BorderLayout.CENTER); 126 txtField = new JTextField(); 127 frame.getContentPane().add(txtField, BorderLayout.NORTH); 128 listener = new bug4624207(); 129 txtField.addFocusListener((FocusListener) listener); 130 frame.pack(); 131 frame.setVisible(true); 132 } 133 }