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 /test/lib
  31  * @library ../../regtesthelpers
  32  * @build Util jdk.test.lib.Platform
  33  * @run main bug4624207
  34  */
  35 import javax.swing.*;
  36 import javax.swing.event.ChangeEvent;
  37 import javax.swing.event.ChangeListener;
  38 import java.awt.*;
  39 import java.awt.event.FocusEvent;
  40 import java.awt.event.FocusListener;
  41 import java.awt.event.KeyEvent;
  42 
  43 import jdk.test.lib.Platform;
  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         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         robot.waitForIdle();
  80 
  81         SwingUtilities.invokeAndWait(new Runnable() {
  82 
  83             public void run() {
  84                 txtField.requestFocus();
  85             }
  86         });
  87 
  88         robot.waitForIdle();
  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         robot.waitForIdle();
 103 
 104         if (Platform.isOSX()) {
 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         robot.waitForIdle();
 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 }