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 ../../../../lib/testlibrary
  30  * @library ../../regtesthelpers
  31  * @build Util jdk.testlibrary.OSInfo
  32  * @run main bug4624207
  33  */
  34 import javax.swing.*;
  35 import javax.swing.event.ChangeEvent;
  36 import javax.swing.event.ChangeListener;
  37 import java.awt.*;
  38 import java.awt.event.FocusEvent;
  39 import java.awt.event.FocusListener;
  40 import java.awt.event.KeyEvent;
  41 
  42 import jdk.testlibrary.OSInfo;
  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         Robot robot = new Robot();
  69         robot.setAutoDelay(50);
  70 
  71         SwingUtilities.invokeAndWait(new Runnable() {
  72 
  73             public void run() {
  74                 createAndShowGUI();
  75             }
  76         });
  77 
  78         robot.waitForIdle();
  79 
  80         SwingUtilities.invokeAndWait(new Runnable() {
  81 
  82             public void run() {
  83                 txtField.requestFocus();
  84             }
  85         });
  86 
  87         robot.waitForIdle();
  88 
  89         if (!focusGained) {
  90             throw new RuntimeException("Couldn't gain focus for text field");
  91         }
  92 
  93         SwingUtilities.invokeAndWait(new Runnable() {
  94 
  95             public void run() {
  96                 tab.addChangeListener((ChangeListener) listener);
  97                 txtField.removeFocusListener((FocusListener) listener);
  98             }
  99         });
 100 
 101         robot.waitForIdle();
 102 
 103         if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {
 104             Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_B);
 105         } else {
 106             Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_B);
 107         }
 108 
 109         robot.waitForIdle();
 110 
 111         if (!stateChanged || tab.getSelectedIndex() != 1) {
 112             throw new RuntimeException("JTabbedPane mnemonics don't work from outside the tabbed pane");
 113         }
 114     }
 115 
 116     private static void createAndShowGUI() {
 117         tab = new JTabbedPane();
 118         tab.add("Tab1", new JButton("Button1"));
 119         tab.add("Tab2", new JButton("Button2"));
 120         tab.setMnemonicAt(0, KeyEvent.VK_T);
 121         tab.setMnemonicAt(1, KeyEvent.VK_B);
 122 
 123         JFrame frame = new JFrame();
 124         frame.getContentPane().add(tab, BorderLayout.CENTER);
 125         txtField = new JTextField();
 126         frame.getContentPane().add(txtField, BorderLayout.NORTH);
 127         listener = new bug4624207();
 128         txtField.addFocusListener((FocusListener) listener);
 129         frame.pack();
 130         frame.setVisible(true);
 131     }
 132 }