Path: blob/master/test/jdk/java/awt/Focus/8282640/ScrollPaneFocusBugTest.java
66645 views
/*1* Copyright (c) 2002, 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 474076126* @key headful27* @summary Focus stays with the ScrollPane despite28* it being removed from the Parent29* @run main ScrollPaneFocusBugTest30*/3132import java.awt.BorderLayout;33import java.awt.Container;34import java.awt.KeyboardFocusManager;35import java.awt.Robot;36import java.awt.event.InputEvent;37import java.awt.event.KeyAdapter;38import java.awt.event.KeyEvent;3940import javax.swing.JComponent;41import javax.swing.JFrame;42import javax.swing.JScrollPane;43import javax.swing.JTextArea;44import javax.swing.JTextField;45import javax.swing.SwingUtilities;4647public class ScrollPaneFocusBugTest {4849private static volatile String focussedComponentName;50private static JScrollPane scrollPane;51private static JFrame frame;52private static Robot robot;5354private static volatile int xLocn;55private static volatile int yLocn;56private static volatile int width;57private static volatile int height;5859public static JScrollPane60createScrollPaneComponent(JComponent componentToMoveFocusTo) {61JTextArea textArea = new JTextArea("1111\n2222\n3333\n4444\n5555\n");62JScrollPane scrollPaneComponent = new JScrollPane(textArea);63textArea.addKeyListener(new KeyAdapter() {64public void keyTyped(KeyEvent e) {65Container parent = scrollPaneComponent.getParent();66parent.remove(scrollPaneComponent);67parent.validate();68parent.repaint();69componentToMoveFocusTo.requestFocus();70}71});72return scrollPaneComponent;73}7475public static void main(String[] args) throws Exception {76SwingUtilities.invokeAndWait(() -> createGUI());7778robot = new Robot();79robot.setAutoDelay(200);80robot.setAutoWaitForIdle(true);8182pressKey();8384SwingUtilities.invokeAndWait(() -> focussedComponentName =85KeyboardFocusManager.getCurrentKeyboardFocusManager()86.getFocusOwner().getClass().getName());8788if (focussedComponentName.equals("javax.swing.JTextField")) {89System.out.println(90"Test Passed: Focus shifted to JTextField"91+ "after removing ScrollPane");92} else {93throw new RuntimeException(94"Test Failed: Focus did not shift to JTextField after"95+ "removing ScrollPane, current"96+ " Focus with " + focussedComponentName);97}98SwingUtilities.invokeAndWait(() -> frame.dispose());99}100101protected static void pressKey() throws Exception {102SwingUtilities.invokeAndWait(() -> {103xLocn = scrollPane.getLocationOnScreen().x;104yLocn = scrollPane.getLocationOnScreen().y;105width = scrollPane.getSize().width;106height = scrollPane.getSize().height;107});108109robot.mouseMove(xLocn + width / 2, yLocn + height / 2);110111robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);112robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);113114robot.keyPress(KeyEvent.VK_1);115robot.keyRelease(KeyEvent.VK_1);116}117118public static void createGUI() {119frame = new JFrame();120frame.setLayout(new BorderLayout());121JTextField textField = new JTextField("Second Component");122frame.add(textField, BorderLayout.SOUTH);123scrollPane = createScrollPaneComponent(textField);124frame.add(scrollPane, BorderLayout.CENTER);125frame.pack();126frame.setLocationRelativeTo(null);127frame.setVisible(true);128}129}130131132133