1 /* 2 * Copyright (c) 2015, 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 8139218 27 @summary Dialog that opens and closes quickly changes focus in original 28 focusowner 29 @author Semyon Sadetsky: area=awt-focus 30 @run main RollbackFocusFromAnotherWindowTest 31 */ 32 33 import java.awt.*; 34 import java.awt.event.*; 35 36 import javax.swing.*; 37 38 public class RollbackFocusFromAnotherWindowTest extends JFrame implements KeyListener 39 { 40 private static RollbackFocusFromAnotherWindowTest tfs; 41 private static Robot robot; 42 43 public static void main(String[] args) throws Exception { 44 robot = new Robot(); 45 46 SwingUtilities.invokeAndWait(() -> { 47 tfs = new RollbackFocusFromAnotherWindowTest(); 48 tfs.setVisible(true); 49 }); 50 51 robot.waitForIdle(); 52 robot.delay(200); 53 54 try { 55 for (int i = 0; i < 10; i++) { 56 robot.keyPress(KeyEvent.VK_A); 57 robot.delay(10); 58 robot.keyRelease(KeyEvent.VK_A); 59 robot.waitForIdle(); 60 robot.delay(200); 61 SwingUtilities.invokeAndWait(() -> { 62 String name = tfs.getFocusOwner().getName(); 63 System.out.println(name); 64 if (!"Comp0".equals(name)) { 65 throw new RuntimeException( 66 "Focus is not restored correctly"); 67 } 68 }); 69 } 70 System.out.println("ok"); 71 } finally { 72 SwingUtilities.invokeLater(() -> tfs.dispose()); 73 } 74 } 75 76 public RollbackFocusFromAnotherWindowTest() 77 { 78 setUndecorated(true); 79 Container c = getContentPane(); 80 c.setLayout(new FlowLayout()); 81 for (int i = 0; i < 10; i++) 82 { 83 JTextField tf = new JTextField("" + i, 10); 84 tf.setName("Comp" + i); 85 c.add(tf); 86 tf.addKeyListener(this); 87 } 88 pack(); 89 } 90 91 @Override 92 public void keyTyped(KeyEvent e) { 93 94 } 95 96 @Override 97 public void keyPressed(KeyEvent e) { 98 Frame frame = new Frame(); 99 frame.setVisible(true); 100 try { 101 Thread.sleep(2); 102 } catch (InterruptedException e1) { 103 e1.printStackTrace(); 104 } 105 frame.dispose(); 106 } 107 108 @Override 109 public void keyReleased(KeyEvent e) { 110 111 } 112 113 114 }