# HG changeset patch # User ssadetsky # Date 1464699421 -10800 # Node ID f52c1429ae8514e231906a598fa55c59fa416f34 # Parent 3fa4e9304a9ab3962d6951b38662b4e6cb666aed 8139218: Dialog that opens and closes quickly changes focus in original focusowner Reviewed-by: alexsch diff -r 3fa4e9304a9a -r f52c1429ae85 jdk/src/java.desktop/share/classes/java/awt/DefaultKeyboardFocusManager.java --- a/jdk/src/java.desktop/share/classes/java/awt/DefaultKeyboardFocusManager.java Tue May 31 16:03:21 2016 +0530 +++ b/jdk/src/java.desktop/share/classes/java/awt/DefaultKeyboardFocusManager.java Tue May 31 15:57:01 2016 +0300 @@ -148,9 +148,20 @@ Component toFocus = KeyboardFocusManager.getMostRecentFocusOwner(aWindow); - if (toFocus != null && toFocus != vetoedComponent && doRestoreFocus(toFocus, vetoedComponent, false)) { - return true; - } else if (clearOnFailure) { + if (toFocus != null && toFocus != vetoedComponent) { + Component heavyweight = getHeavyweight(aWindow); + if (heavyweight != null) { + setNativeFocusOwner(heavyweight); + Toolkit.getEventQueue().createSecondaryLoop( + () -> getGlobalFocusedWindow() != aWindow, null, 50) + .enter(); + } + if (getGlobalFocusedWindow() == aWindow && + doRestoreFocus(toFocus, vetoedComponent, false)) { + return true; + } + } + if (clearOnFailure) { clearGlobalFocusOwnerPriv(); return true; } else { diff -r 3fa4e9304a9a -r f52c1429ae85 jdk/test/java/awt/Focus/RollbackFocusFromAnotherWindowTest/RollbackFocusFromAnotherWindowTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/awt/Focus/RollbackFocusFromAnotherWindowTest/RollbackFocusFromAnotherWindowTest.java Tue May 31 15:57:01 2016 +0300 @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + @test + @bug 8139218 + @summary Dialog that opens and closes quickly changes focus in original + focusowner + @run main RollbackFocusFromAnotherWindowTest + */ + +import java.awt.*; +import java.awt.event.*; + +import javax.swing.*; + +public class RollbackFocusFromAnotherWindowTest extends JFrame + implements KeyListener +{ + private static RollbackFocusFromAnotherWindowTest tfs; + private static Robot robot; + + public static void main(String[] args) throws Exception { + robot = new Robot(); + + SwingUtilities.invokeAndWait(() -> { + tfs = new RollbackFocusFromAnotherWindowTest(); + tfs.setVisible(true); + }); + + robot.waitForIdle(); + robot.delay(200); + + try { + for (int i = 0; i < 10; i++) { + robot.keyPress(KeyEvent.VK_A); + robot.delay(10); + robot.keyRelease(KeyEvent.VK_A); + robot.waitForIdle(); + robot.delay(200); + SwingUtilities.invokeAndWait(() -> { + String name = tfs.getFocusOwner().getName(); + System.out.println(name); + if (!"Comp0".equals(name)) { + throw new RuntimeException( + "Focus is not restored correctly"); + } + }); + } + System.out.println("ok"); + } finally { + SwingUtilities.invokeLater(() -> tfs.dispose()); + } + } + + public RollbackFocusFromAnotherWindowTest() + { + setUndecorated(true); + Container c = getContentPane(); + c.setLayout(new FlowLayout()); + for (int i = 0; i < 10; i++) + { + JTextField tf = new JTextField("" + i, 10); + tf.setName("Comp" + i); + c.add(tf); + tf.addKeyListener(this); + } + pack(); + } + + @Override + public void keyTyped(KeyEvent e) { + + } + + @Override + public void keyPressed(KeyEvent e) { + Frame frame = new Frame(); + frame.setVisible(true); + try { + Thread.sleep(2); + } catch (InterruptedException e1) { + e1.printStackTrace(); + } + frame.dispose(); + } + + @Override + public void keyReleased(KeyEvent e) { + + } +}