--- old/src/macosx/classes/sun/lwawt/macosx/CEmbeddedFrame.java 2014-07-03 12:12:03.000000000 +0400 +++ new/src/macosx/classes/sun/lwawt/macosx/CEmbeddedFrame.java 2014-07-03 12:12:03.000000000 +0400 @@ -39,6 +39,7 @@ private CPlatformResponder responder; private static final Object classLock = new Object(); private static volatile CEmbeddedFrame focusedWindow; + private CEmbeddedFrame previousFocusedWindow; private boolean parentWindowActive = true; public CEmbeddedFrame() { @@ -145,6 +146,25 @@ // non-focused applet. This method can be called from different threads. public void handleWindowFocusEvent(boolean parentWindowActive) { this.parentWindowActive = parentWindowActive; + // If several applets are running in different browser's windows, it is necessary to + // detect the switching between the parent windows and update focusedWindow accordingly. + synchronized (classLock) { + if (!parentWindowActive) { + this.previousFocusedWindow = focusedWindow; + } + if (parentWindowActive && focusedWindow != this && + (focusedWindow == null || (focusedWindow != null && !focusedWindow.isParentWindowActive()))) { + // It looks like we have switched to another browser window, let's restore focus to + // the previously focused applet in this window. + if (this.previousFocusedWindow != null) { + focusedWindow = this.previousFocusedWindow; + } else { + // No applets were focused in this window, so set focus to the first + // applet in the window + focusedWindow = this; + } + } + } // ignore focus "lost" native request as it may mistakenly // deactivate active window (see 8001161) if (focusedWindow == this && parentWindowActive) {