Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4781423

Alt Key Errors and NullPointerException After Removal of Container with Focus

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P3 P3
    • None
    • 1.4.1, 1.4.2
    • client-libs
    • x86
    • windows_2000

      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)
      ======================================================================

            son Oleg Sukhodolsky (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: