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

JMenuBar.removeAll() then add() breaks menus

    Details

      Description

      Compile the following program and follow the instructions in comments how to reproduce the bug.

      --------------------------
      import java.awt.*;
      import java.awt.event.*;

      import javax.swing.JButton;
      import javax.swing.JFrame;
      import javax.swing.JMenu;
      import javax.swing.JMenuItem;
      import javax.swing.JMenuBar;

      /*
       * Test class to show problem removing then adding a menu to a JMenuBar.
       * The problem only occurs when *not* running with
      useScreenMenuBar="true"
      *
      * To reproduce:
      * 1. Compile with "javac R3289768.java"
      * 2. Run with "java -cp . R3289768"
      * 3. Open the menu in the Frame. You should see one menu item. Click
      the
      * button, and then open the menu again. The menu doesn't open the
      second
      * time.
      * 4. Quit and run again, this time using
      apple.laf.useScreenMenuBar="true".
      * Open the menu; it works. Press the button, and open the menu
      again.
      * It works correctly.
      *
      * @author Steve Magoun (###@###.###)
      */
      public class R3289768 extends JFrame
          implements ActionListener {
          JMenuBar menuBar;

          public R3289768() {
              JButton button = new JButton("Click me to refresh menus");
              button.addActionListener(this);
              getContentPane().add(button);

              menuBar = new JMenuBar();
              menuBar.add(getMenu());
              setJMenuBar(menuBar);
          }

          public void actionPerformed(ActionEvent e) {
              menuBar.removeAll();
              menuBar.add(getMenu());
              // Uncomment this line for proper behavior when *not*
              // using apple.laf.useScreenMenuBar="true"
              // setJMenuBar(menuBar);
          }

          protected JMenu getMenu() {
              JMenu menu = new JMenu("A Menu");
              JMenuItem menuItem = new JMenuItem("A menu item");
              menu.add(menuItem);
              return menu;
          }

          public static void main(String[] args) {
              R3289768 window = new R3289768();

              window.setTitle("R3289768");
              window.setSize(450, 260);
              window.setVisible(true);
          }
      }
      --------------------

        Attachments

          Activity

            People

            • Assignee:
              vdyakov Victor Dyakov
              Reporter:
              mbykov Misha Bykov
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Imported:
                Indexed: