--- old/src/macosx/classes/sun/lwawt/LWComponentPeer.java 2012-11-29 00:56:06.000000000 +0400 +++ new/src/macosx/classes/sun/lwawt/LWComponentPeer.java 2012-11-29 00:56:06.000000000 +0400 @@ -1251,9 +1251,6 @@ if (!target.isFocusOwner() && LWKeyboardFocusManagerPeer.shouldFocusOnClick(target)) { LWKeyboardFocusManagerPeer.requestFocusFor(target, CausedFocusEvent.Cause.MOUSE_EVENT); - } else { - // Anyway request focus to the toplevel. - getWindowPeerOrSelf().requestWindowFocus(CausedFocusEvent.Cause.MOUSE_EVENT); } } --- old/src/macosx/classes/sun/lwawt/LWWindowPeer.java 2012-11-29 00:56:08.000000000 +0400 +++ new/src/macosx/classes/sun/lwawt/LWWindowPeer.java 2012-11-29 00:56:07.000000000 +0400 @@ -811,6 +811,14 @@ mouseClickButtons |= eventButtonMask; } + // The window should be focused on mouse click. If it gets activated by the native platform, + // this request will be no op. It will take effect when: + // 1. A simple not focused window is clicked. + // 2. An active but not focused owner frame/dialog is clicked. + // The mouse event then will trigger a focus request "in window" to the component, so the window + // should gain focus before. + requestWindowFocus(CausedFocusEvent.Cause.MOUSE_EVENT); + mouseDownTarget[targetIdx] = targetPeer; } else if (id == MouseEvent.MOUSE_DRAGGED) { // Cocoa dragged event has the information about which mouse @@ -901,20 +909,16 @@ public void dispatchKeyEvent(int id, long when, int modifiers, int keyCode, char keyChar, int keyLocation) { - KeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance(); + LWKeyboardFocusManagerPeer kfmPeer = LWKeyboardFocusManagerPeer.getInstance(); Component focusOwner = kfmPeer.getCurrentFocusOwner(); - // Null focus owner may receive key event when - // application hides the focused window upon ESC press - // (AWT transfers/clears the focus owner) and pending ESC release - // may come to already hidden window. This check eliminates NPE. - if (focusOwner != null) { - KeyEvent event = - new KeyEvent(focusOwner, id, when, modifiers, - keyCode, keyChar, keyLocation); - LWComponentPeer peer = (LWComponentPeer)focusOwner.getPeer(); - peer.postEvent(event); + if (focusOwner == null) { + focusOwner = kfmPeer.getCurrentFocusedWindow(); + if (focusOwner == null) { + focusOwner = this.getTarget(); + } } + postEvent(new KeyEvent(focusOwner, id, when, modifiers, keyCode, keyChar, keyLocation)); }