Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/awt/Focus/8282640/ScrollPaneFocusBugTest.java
66645 views
1
/*
2
* Copyright (c) 2002, 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 4740761
27
* @key headful
28
* @summary Focus stays with the ScrollPane despite
29
* it being removed from the Parent
30
* @run main ScrollPaneFocusBugTest
31
*/
32
33
import java.awt.BorderLayout;
34
import java.awt.Container;
35
import java.awt.KeyboardFocusManager;
36
import java.awt.Robot;
37
import java.awt.event.InputEvent;
38
import java.awt.event.KeyAdapter;
39
import java.awt.event.KeyEvent;
40
41
import javax.swing.JComponent;
42
import javax.swing.JFrame;
43
import javax.swing.JScrollPane;
44
import javax.swing.JTextArea;
45
import javax.swing.JTextField;
46
import javax.swing.SwingUtilities;
47
48
public class ScrollPaneFocusBugTest {
49
50
private static volatile String focussedComponentName;
51
private static JScrollPane scrollPane;
52
private static JFrame frame;
53
private static Robot robot;
54
55
private static volatile int xLocn;
56
private static volatile int yLocn;
57
private static volatile int width;
58
private static volatile int height;
59
60
public static JScrollPane
61
createScrollPaneComponent(JComponent componentToMoveFocusTo) {
62
JTextArea textArea = new JTextArea("1111\n2222\n3333\n4444\n5555\n");
63
JScrollPane scrollPaneComponent = new JScrollPane(textArea);
64
textArea.addKeyListener(new KeyAdapter() {
65
public void keyTyped(KeyEvent e) {
66
Container parent = scrollPaneComponent.getParent();
67
parent.remove(scrollPaneComponent);
68
parent.validate();
69
parent.repaint();
70
componentToMoveFocusTo.requestFocus();
71
}
72
});
73
return scrollPaneComponent;
74
}
75
76
public static void main(String[] args) throws Exception {
77
SwingUtilities.invokeAndWait(() -> createGUI());
78
79
robot = new Robot();
80
robot.setAutoDelay(200);
81
robot.setAutoWaitForIdle(true);
82
83
pressKey();
84
85
SwingUtilities.invokeAndWait(() -> focussedComponentName =
86
KeyboardFocusManager.getCurrentKeyboardFocusManager()
87
.getFocusOwner().getClass().getName());
88
89
if (focussedComponentName.equals("javax.swing.JTextField")) {
90
System.out.println(
91
"Test Passed: Focus shifted to JTextField"
92
+ "after removing ScrollPane");
93
} else {
94
throw new RuntimeException(
95
"Test Failed: Focus did not shift to JTextField after"
96
+ "removing ScrollPane, current"
97
+ " Focus with " + focussedComponentName);
98
}
99
SwingUtilities.invokeAndWait(() -> frame.dispose());
100
}
101
102
protected static void pressKey() throws Exception {
103
SwingUtilities.invokeAndWait(() -> {
104
xLocn = scrollPane.getLocationOnScreen().x;
105
yLocn = scrollPane.getLocationOnScreen().y;
106
width = scrollPane.getSize().width;
107
height = scrollPane.getSize().height;
108
});
109
110
robot.mouseMove(xLocn + width / 2, yLocn + height / 2);
111
112
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
113
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
114
115
robot.keyPress(KeyEvent.VK_1);
116
robot.keyRelease(KeyEvent.VK_1);
117
}
118
119
public static void createGUI() {
120
frame = new JFrame();
121
frame.setLayout(new BorderLayout());
122
JTextField textField = new JTextField("Second Component");
123
frame.add(textField, BorderLayout.SOUTH);
124
scrollPane = createScrollPaneComponent(textField);
125
frame.add(scrollPane, BorderLayout.CENTER);
126
frame.pack();
127
frame.setLocationRelativeTo(null);
128
frame.setVisible(true);
129
}
130
}
131
132
133