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