Path: blob/master/test/jdk/javax/swing/JTabbedPane/4624207/bug4624207.java
41153 views
/*1* Copyright (c) 2011, 2018, 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* @key headful26* @bug 462420727* @summary JTabbedPane mnemonics don't work from outside the tabbed pane28* @author Oleg Mokhovikov29* @library /test/lib30* @library ../../regtesthelpers31* @build Util jdk.test.lib.Platform32* @run main bug462420733*/34import javax.swing.*;35import javax.swing.event.ChangeEvent;36import javax.swing.event.ChangeListener;37import java.awt.*;38import java.awt.event.FocusEvent;39import java.awt.event.FocusListener;40import java.awt.event.KeyEvent;4142import jdk.test.lib.Platform;4344public class bug4624207 implements ChangeListener, FocusListener {4546private static volatile boolean stateChanged = false;47private static volatile boolean focusGained = false;48private static JTextField txtField;49private static JTabbedPane tab;50private static Object listener;51private static JFrame frame;5253public void stateChanged(ChangeEvent e) {54System.out.println("stateChanged called");55stateChanged = true;56}5758public void focusGained(FocusEvent e) {59System.out.println("focusGained called");60focusGained = true;61}6263public void focusLost(FocusEvent e) {64System.out.println("focusLost called");65focusGained = false;66}6768public static void main(String[] args) throws Exception {69try {70Robot robot = new Robot();71robot.setAutoDelay(50);7273SwingUtilities.invokeAndWait(new Runnable() {7475public void run() {76createAndShowGUI();77}78});7980robot.waitForIdle();8182SwingUtilities.invokeAndWait(new Runnable() {8384public void run() {85txtField.requestFocus();86}87});8889robot.waitForIdle();9091if (!focusGained) {92throw new RuntimeException("Couldn't gain focus for text field");93}9495SwingUtilities.invokeAndWait(new Runnable() {9697public void run() {98tab.addChangeListener((ChangeListener) listener);99txtField.removeFocusListener((FocusListener) listener);100}101});102103robot.waitForIdle();104105if (Platform.isOSX()) {106Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_B);107} else {108Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_B);109}110111robot.waitForIdle();112113if (!stateChanged || tab.getSelectedIndex() != 1) {114throw new RuntimeException("JTabbedPane mnemonics don't work from outside the tabbed pane");115}116} finally {117if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());118}119}120121private static void createAndShowGUI() {122tab = new JTabbedPane();123tab.add("Tab1", new JButton("Button1"));124tab.add("Tab2", new JButton("Button2"));125tab.setMnemonicAt(0, KeyEvent.VK_T);126tab.setMnemonicAt(1, KeyEvent.VK_B);127128frame = new JFrame();129frame.getContentPane().add(tab, BorderLayout.CENTER);130txtField = new JTextField();131frame.getContentPane().add(txtField, BorderLayout.NORTH);132listener = new bug4624207();133txtField.addFocusListener((FocusListener) listener);134frame.pack();135frame.setVisible(true);136}137}138139140