Path: blob/master/test/jdk/java/awt/Focus/8000326/SetFocusTraversalKeysEnabledTest.java
66645 views
/*1* Copyright (c) 2013, 2022, 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 800032626* @key headful27* @summary Focus unable to traverse within the menubar28* @run main SetFocusTraversalKeysEnabledTest29*/30import java.awt.AWTException;31import java.awt.BorderLayout;32import java.awt.Component;33import java.awt.ContainerOrderFocusTraversalPolicy;34import java.awt.Robot;35import java.awt.event.KeyEvent;36import java.lang.reflect.InvocationTargetException;3738import javax.swing.JButton;39import javax.swing.JFrame;40import javax.swing.JMenu;41import javax.swing.JMenuBar;42import javax.swing.JMenuItem;43import javax.swing.JPanel;44import javax.swing.SwingUtilities;4546public class SetFocusTraversalKeysEnabledTest {4748private static volatile JFrame jFrame;49private static volatile Component currentFocusOwner;5051private static void doTest()52throws InvocationTargetException, InterruptedException, AWTException {53try {54SwingUtilities.invokeAndWait(() -> createGUI());55Robot robot = new Robot();56robot.setAutoDelay(500);57Component lastFocusOwner = null;58do {59robot.waitForIdle();60SwingUtilities.invokeAndWait(() -> currentFocusOwner = jFrame.getFocusOwner());6162System.out.println("Focus owner is : " + currentFocusOwner.getClass().getName());63if (currentFocusOwner == lastFocusOwner) {64throw new RuntimeException(65"Problem moving focus from " + currentFocusOwner.getClass().getName());66}67lastFocusOwner = currentFocusOwner;68robot.keyPress(KeyEvent.VK_TAB);69robot.keyRelease(KeyEvent.VK_TAB);70} while (currentFocusOwner != jFrame);71} finally {72SwingUtilities.invokeAndWait(() -> jFrame.dispose());73}74}7576private static void createGUI() {77jFrame = new JFrame("Focus Traversal Test");78jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);7980JMenuBar jMenuBar = new JMenuBar();81jMenuBar.setFocusTraversalKeysEnabled(true);82jMenuBar.add(new JMenu("First Menu").add(new JMenuItem("First MenuItem")));8384JButton northButton = new JButton("North Button");85JButton southButton = new JButton("South Button");8687JPanel jPanel = new JPanel(new BorderLayout());88jPanel.add(northButton);89jPanel.add(jMenuBar, BorderLayout.NORTH);90jPanel.add(southButton, BorderLayout.SOUTH);9192jFrame.getContentPane().add(jPanel);93jFrame.setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy());94jFrame.pack();95northButton.requestFocusInWindow();96jFrame.setLocationRelativeTo(null);97jFrame.setVisible(true);98}99100public static void main(String[] args) throws Exception {101doTest();102}103}104105106107