JDK-4841881 : Alt tab with Windows L&F moves focus to the application menubar
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.2
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-04-03
  • Updated: 2006-04-05
  • Resolved: 2006-04-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
6 b79Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Name: ks84122			Date: 04/02/2003

The problem below is reproducible with JDK 1.4.1 and 1.4.2 build 19 on windows 2000. The problem isn't reproducible with some of the earlier builds of 1.4.2 (build 03, for example). However, it seems to have resurfaced again in the latest builds.

Original description from Borland:

Very frequently only with the Windows Look and Feel doing an Alt-Tab from one application to another and back results in focus being moved to the first menu item.

Run the program below.  Focus starts out in the text field.  Use Alt-Tab to move to another application (e.g. Notepad) and back multiple times. Usually by the second or third time the File menu item is selected.  On some machines happens every time.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Frame1 extends JFrame {
  JPanel contentPane;
  JMenuBar jMenuBar1 = new JMenuBar();
  JMenu jMenuFile = new JMenu();
  JMenuItem jMenuFileExit = new JMenuItem();
  JMenu jMenuHelp = new JMenu();
  JMenuItem jMenuHelpAbout = new JMenuItem();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  JLabel jLabel1 = new JLabel();
  JTextField jTextField1 = new JTextField();

  //Construct the frame
  public Frame1() {
    contentPane = (JPanel) this.getContentPane();

    this.setTitle("Frame Title");




    contentPane.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
      GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
    contentPane.add(jTextField1,  new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
      GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));

  public static void main(String[] args) {
    try {
    catch (Exception ex) {

    Frame1 frame = new Frame1();
    frame.setSize(200, 100);
    frame.setLocation(200, 200);
(Review ID: 165756) 
Please refer to 6458497. I have verified (on mustang build 93) a report that highlights that the focus still stays on the menu bar. This can be reproduced with the test case in 4841881 by quickly pressing the ALT-TAB buttons.

SUGGESTED FIX --- awt_Window.cpp 2006-03-04 11:35:13.000000000 +0300 *************** *** 156,165 **** } m_grabbed = TRUE; m_grabbedWindow = this; ! if (GetHWnd() != sm_focusedWindow) { _ToFront(env->NewGlobalRef(GetPeer(env))); } - // Global ref was deleted in _ToFront } void --- 156,168 ---- } m_grabbed = TRUE; m_grabbedWindow = this; ! if (sm_focusedWindow == NULL) { ! // we shouldn't perform grab in this case ! Ungrab(); ! } else if (GetHWnd() != sm_focusedWindow) { _ToFront(env->NewGlobalRef(GetPeer(env))); + // Global ref was deleted in _ToFront } }

EVALUATION the problem is that grab() is called when the frame is deactivated (and new active window is not one of its owned windows). I think we should send ungrab event in this situation and grab nothing.

EVALUATION When we show heavyweight popup Swing graps the window, if we quickly do alt+tab in this case sometimes ungrab event doesn't come and JMenu stay selected reassigned to AWT

EVALUATION I've reopened this as it is reproducible with 1.6 and is causing grief with a number of apps, including NetBeans. Take a look at http://www.netbeans.org/issues/show_bug.cgi?id=46826 for a list of the NetBeans customers that have reported this. I'll also say that the problem is a worse then outlined in the description. In particular with NetBeans in some cases after releasing the alt or tab keys focus snaps back to NetBeans!

EVALUATION Name: azR10139 Date: 04/07/2003 This is another side-effect of fix for bug 4766477. In the fix we are removing the code that cancels popup on deactivation of the invoker's frame. What happens now: the user presses <ALT>, then <TAB>, but we are receiving only <ALT> key pressing event. If user releases <ALT> before releasing <TAB> we will receive <ALT> release event and activate menu. After this frame losts focus and deactivates. Without the fix for bug 4766477 the code that detects deactivation of the frame cancels popup by setting empty selected path in MenuSelectionManager. Then menu is becames deselected and focus returns to where it was before menu is activated. User does even feels that menu was activated. This bug will be fixed in the next release. ###@###.### 04/07/2003 ====================================================================== Can not be reproduced on all 1.5 builds. Closeng as not reproducible. ###@###.### 2003-09-19