JDK-4781423 : Alt Key Errors and NullPointerException After Removal of Container with Focus
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.1,1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-11-19
  • Updated: 2007-06-07
  • Resolved: 2007-06-07
Related Reports
Duplicate :  
Relates :  
Relates :  
Description
Name: rmT116609			Date: 11/19/2002


FULL PRODUCT VERSION :
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)

FULL OPERATING SYSTEM VERSION : Microsoft Windows 2000
[Version 5.00.2195]


A DESCRIPTION OF THE PROBLEM :
In certain cases where a Container is removed from a parent
Container and the child container contains a component that
has the focus, if the Alt key is subsequently pressed a
NullPointerException occurs in
SwingUtilities.getWindowAncestor when called from
WindowsPopupMenuUI.AltProcessor.altReleased. In addition,
after removing the cause of the NPE, the menubar does not
respond to the Alt key.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Begin execution of the AltError program provided.
2. Press the Alt key once the frame appears.


EXPECTED VERSUS ACTUAL BEHAVIOR :
The menu in the menubar should appear to be selected.
Instead, the NPE occurs. Moreover, once the NPE is
corrected, there is still no response in the MenuBar.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java.lang.NullPointerException
        at javax.swing.SwingUtilities.getWindowAncestor(SwingUtilities.java:63)
        at
com.sun.java.swing.plaf.windows.WindowsPopupMenuUI$AltProcessor.altReleased
(WindowsPopupMenuUI.java:115)
        at
com.sun.java.swing.plaf.windows.WindowsPopupMenuUI$AltProcessor.postProcessKeyEv
ent(WindowsPopupMenuUI.java:138)
        at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent
(DefaultKeyboardFocusManager.java:603)
        at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent
(DefaultKeyboardFocusManager.java:765)
        at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions
(DefaultKeyboardFocusManager.java:698)
        at java.awt.DefaultKeyboardFocusManager.dispatchEvent
(DefaultKeyboardFocusManager.java:559)
        at java.awt.Component.dispatchEventImpl(Component.java:3468)
        at java.awt.Container.dispatchEventImpl(Container.java:1623)
        at java.awt.Window.dispatchEventImpl(Window.java:1585)
        at java.awt.Component.dispatchEvent(Component.java:3439)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy
(EventDispatchThread.java:197)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy
(EventDispatchThread.java:150)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
^C


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AltError extends JFrame {
 public AltError() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.
    getKeyStroke(KeyEvent.VK_ALT, Event.ALT_MASK, false), "repaint");
  setBounds(new Rectangle(100,100,300,200));
  final JMenuBar menuBar = new JMenuBar();
  final JMenu menu = new javax.swing.JMenu("Menu");
// The following is a workaround for this case but not others
//  menu.setFocusable(true);
  final JMenuItem menuItem = new JMenuItem("Menu Item");
  menu.add(menuItem);
  menuBar.add(menu);
  setJMenuBar(menuBar);
  final JPanel panel1 = new JPanel();
  final JPanel panel2 = new JPanel();
  final JButton button1 = new JButton("1");
  panel2.add(button1);
  panel2.add(new JButton("2"));
  panel1.add(panel2);
  getContentPane().add(panel1);
  show();
  SwingUtilities.invokeLater(new Runnable() {
      public final void run() {
// The following seems to be a workaround for all cases
//        panel1.setVisible(false);
        panel1.remove(panel2);
        panel1.revalidate();
        panel1.repaint();
      }
  });
 }
 public static final void main(final String[] args) {
  try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  }
  catch(Throwable e) {}
  new AltError();
 }
}

---------- END SOURCE ----------

CUSTOMER WORKAROUND :
The NPE occurs when
com.sun.java.swing.plaf.windows.WindowsPopupMenuUI.AltProces
sor.altReleased() calls getWindowAncestor(), yet this call
appears to serve no purpose (see below), so one fix appears
to be the removal of this call in a replacement version of
this class.

        void altReleased(KeyEvent ev) {
            if (menuCanceledOnPress) {
                return;
            }

            MenuSelectionManager msm =
                MenuSelectionManager.defaultManager();
            if (msm.getSelectedPath().length == 0) {
                // if no menu is active, we try activating
the menubar
                JRootPane root = SwingUtilities.getRootPane
(ev.getComponent());
// The following line seems to have no purpose
//                java.awt.Window w =
SwingUtilities.getWindowAncestor(root);

                JMenuBar mbar = root != null ?
root.getJMenuBar() : null;
                JMenu menu = mbar != null ? mbar.getMenu
(0) : null;

                if (menu != null) {
                    MenuElement[] path = new MenuElement[2];
                    path[0] = mbar;
                    path[1] = menu;
                    msm.setSelectedPath(path);
                }
            }
        }

Nevertheless, removing the call does not cause the Alt key
to respond as expected. A workaround seems to be to hide
the child container before removing it from the parent
container.
(Review ID: 167164) 
======================================================================

Comments
EVALUATION was unable to reproduce the problem with 6.0 and latest buils of 7.0 :( so closing it :)
07-06-2007

EVALUATION Name: osR10079 Date: 11/25/2002 The problem is as follows: when we hide container which contains focus owner, we try to perform focus transfer. We do this in Component.removeNotify() if current component is focus owner. In our case button1 is focus owner and when we call removeNotify() on it, focus is transfered to button2 which at this moment stil has peer and accepted by focus traversal policy. After that we call removeNotify() on button2, but it isn't a focus owner because focus transfer is asynchronous. Than FOCUS_GAINED for button2 is received and button2 become a focus owner (possibly this is incorrect but the cause of our problem is incorrect focus transfer). Than all KeyEvents dispatched by button2 which is removed. And so, swing throws NPE. ###@###.### 2002-11-26 ======================================================================
26-11-2002