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

JMenu fails to override addImpl

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.6.0_03"
      Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
      Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      JMenu overrides add(Component) and add(Component, int). Making calls to add(Component, Object), add(Component, Object, int) or add(String, Component) fail to perform correctly. (There are no issues with JMenu.add methods that are not members of Container.)

      The overridden add methods each call ensurePopupMenuCreated before delegating the addition to the internal JPopupMenu. When calling any of the non-overridden Container.add methods, the items are added to the JMenu and not to the delegate JPopupMenu.

      Instead of overriding add, JMenu should override addImpl. This will ensure that any Container.add method will be correctly delegated to the JPopupMenu.

      Furthermore, the current situation also has the potential for a memory leak, since it is possible to add components to the JMenu using the non-overridden methods, but it is impossible to remove them, since all of JMenu's remove methods have been overridden to delegate to the JPopupMenu.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use any of the non-overridden Container.add methods.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      All add methods will delegate to the internal JPopupMenu.
      ACTUAL -
      Only the overridden add methods delegate to the internal JPopupMenu.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JMenu;
      import javax.swing.JMenuBar;
      import javax.swing.JMenuItem;
      import javax.swing.SwingUtilities;

      /**
       * JMenu test case.
       */
      public class JMenuTestCase extends JFrame {
          /**
           * {@inheritDoc}
           */
          protected void frameInit() {
              super.frameInit();
              
              setDefaultCloseOperation(EXIT_ON_CLOSE);
              
              JMenu menu = new JMenu("Bad Menu");
              menu.add(new JMenuItem("A"));
              System.err.println("Added A, count = " + menu.getComponentCount());
              menu.add(new JMenuItem("B"), 0);
              System.err.println("Added B, count = " + menu.getComponentCount());
              menu.add(new JMenuItem("C"), null);
              System.err.println("Added C, count = " + menu.getComponentCount());
              menu.add(new JMenuItem("D"), null, 0);
              System.err.println("Added D, count = " + menu.getComponentCount());
              menu.add("Random String", new JMenuItem("E"));
              System.err.println("Added E, count = " + menu.getComponentCount());
              
              JMenuBar menubar = new JMenuBar();
              menubar.add(menu);
              setJMenuBar(menubar);
              
              add(new JLabel("Some content"));
              
              pack();
          }
          
          /**
           * @param args
           * unused
           */
          public static void main(String args[]) {
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      new JMenuTestCase().setVisible(true);
                  }
              });
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Don't use the non-overridden Container.add methods.

            Unassigned Unassigned
            igor Igor Nekrestyanov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: