Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/awt/Focus/8000326/SetFocusTraversalKeysEnabledTest.java
66645 views
1
/*
2
* Copyright (c) 2013, 2022, 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
* @bug 8000326
27
* @key headful
28
* @summary Focus unable to traverse within the menubar
29
* @run main SetFocusTraversalKeysEnabledTest
30
*/
31
import java.awt.AWTException;
32
import java.awt.BorderLayout;
33
import java.awt.Component;
34
import java.awt.ContainerOrderFocusTraversalPolicy;
35
import java.awt.Robot;
36
import java.awt.event.KeyEvent;
37
import java.lang.reflect.InvocationTargetException;
38
39
import javax.swing.JButton;
40
import javax.swing.JFrame;
41
import javax.swing.JMenu;
42
import javax.swing.JMenuBar;
43
import javax.swing.JMenuItem;
44
import javax.swing.JPanel;
45
import javax.swing.SwingUtilities;
46
47
public class SetFocusTraversalKeysEnabledTest {
48
49
private static volatile JFrame jFrame;
50
private static volatile Component currentFocusOwner;
51
52
private static void doTest()
53
throws InvocationTargetException, InterruptedException, AWTException {
54
try {
55
SwingUtilities.invokeAndWait(() -> createGUI());
56
Robot robot = new Robot();
57
robot.setAutoDelay(500);
58
Component lastFocusOwner = null;
59
do {
60
robot.waitForIdle();
61
SwingUtilities.invokeAndWait(() -> currentFocusOwner = jFrame.getFocusOwner());
62
63
System.out.println("Focus owner is : " + currentFocusOwner.getClass().getName());
64
if (currentFocusOwner == lastFocusOwner) {
65
throw new RuntimeException(
66
"Problem moving focus from " + currentFocusOwner.getClass().getName());
67
}
68
lastFocusOwner = currentFocusOwner;
69
robot.keyPress(KeyEvent.VK_TAB);
70
robot.keyRelease(KeyEvent.VK_TAB);
71
} while (currentFocusOwner != jFrame);
72
} finally {
73
SwingUtilities.invokeAndWait(() -> jFrame.dispose());
74
}
75
}
76
77
private static void createGUI() {
78
jFrame = new JFrame("Focus Traversal Test");
79
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
80
81
JMenuBar jMenuBar = new JMenuBar();
82
jMenuBar.setFocusTraversalKeysEnabled(true);
83
jMenuBar.add(new JMenu("First Menu").add(new JMenuItem("First MenuItem")));
84
85
JButton northButton = new JButton("North Button");
86
JButton southButton = new JButton("South Button");
87
88
JPanel jPanel = new JPanel(new BorderLayout());
89
jPanel.add(northButton);
90
jPanel.add(jMenuBar, BorderLayout.NORTH);
91
jPanel.add(southButton, BorderLayout.SOUTH);
92
93
jFrame.getContentPane().add(jPanel);
94
jFrame.setFocusTraversalPolicy(new ContainerOrderFocusTraversalPolicy());
95
jFrame.pack();
96
northButton.requestFocusInWindow();
97
jFrame.setLocationRelativeTo(null);
98
jFrame.setVisible(true);
99
}
100
101
public static void main(String[] args) throws Exception {
102
doTest();
103
}
104
}
105
106
107