Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTabbedPane/4624207/bug4624207.java
38855 views
/*1* Copyright (c) 2011, 2013, 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* @bug 462420726* @summary JTabbedPane mnemonics don't work from outside the tabbed pane27* @author Oleg Mokhovikov28* @library ../../../../lib/testlibrary29* @library ../../regtesthelpers30* @build Util jdk.testlibrary.OSInfo31* @run main bug462420732*/33import javax.swing.*;34import javax.swing.event.ChangeEvent;35import javax.swing.event.ChangeListener;36import java.awt.*;37import java.awt.event.FocusEvent;38import java.awt.event.FocusListener;39import java.awt.event.KeyEvent;4041import jdk.testlibrary.OSInfo;4243public class bug4624207 implements ChangeListener, FocusListener {4445private static volatile boolean stateChanged = false;46private static volatile boolean focusGained = false;47private static JTextField txtField;48private static JTabbedPane tab;49private static Object listener;5051public void stateChanged(ChangeEvent e) {52System.out.println("stateChanged called");53stateChanged = true;54}5556public void focusGained(FocusEvent e) {57System.out.println("focusGained called");58focusGained = true;59}6061public void focusLost(FocusEvent e) {62System.out.println("focusLost called");63focusGained = false;64}6566public static void main(String[] args) throws Exception {67Robot robot = new Robot();68robot.setAutoDelay(50);6970SwingUtilities.invokeAndWait(new Runnable() {7172public void run() {73createAndShowGUI();74}75});7677robot.waitForIdle();7879SwingUtilities.invokeAndWait(new Runnable() {8081public void run() {82txtField.requestFocus();83}84});8586robot.waitForIdle();8788if (!focusGained) {89throw new RuntimeException("Couldn't gain focus for text field");90}9192SwingUtilities.invokeAndWait(new Runnable() {9394public void run() {95tab.addChangeListener((ChangeListener) listener);96txtField.removeFocusListener((FocusListener) listener);97}98});99100robot.waitForIdle();101102if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {103Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_B);104} else {105Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_B);106}107108robot.waitForIdle();109110if (!stateChanged || tab.getSelectedIndex() != 1) {111throw new RuntimeException("JTabbedPane mnemonics don't work from outside the tabbed pane");112}113}114115private static void createAndShowGUI() {116tab = new JTabbedPane();117tab.add("Tab1", new JButton("Button1"));118tab.add("Tab2", new JButton("Button2"));119tab.setMnemonicAt(0, KeyEvent.VK_T);120tab.setMnemonicAt(1, KeyEvent.VK_B);121122JFrame frame = new JFrame();123frame.getContentPane().add(tab, BorderLayout.CENTER);124txtField = new JTextField();125frame.getContentPane().add(txtField, BorderLayout.NORTH);126listener = new bug4624207();127txtField.addFocusListener((FocusListener) listener);128frame.pack();129frame.setVisible(true);130}131}132133134