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

Frame.setJMenuBar doesn't work properly

XMLWordPrintable

      Name: rmT116609 Date: 11/05/2003


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

      FULL OS VERSION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      We have an application that displays many panels, each with it's own menu bar. When we display a panel we display the corresponding menu bar using Frame.setJMenuBar. The menu doesn't change on the second or any subsequent call to setJMenuBar.



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a JFrame which contains a button.
      2. Create 2 different menubars, say A and B
      3. Set the frames menu bar to A initially
      4. Add an action to the button to set the frame's menubar to B
      5. Compile and run.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Menu bar A should be displayed until the button is pushed. After the button is pushed, menubar B should be displayed.
      ACTUAL -
      Menubar A is always displayed, even after the button push. Strange behavior occurs when the frame is repainted, sometimes it will display the menu, sometimes it will display the button text.

      REPRODUCIBILITY :
      This bug can be reproduced always.

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

      import javax.swing.*;

      public class MenuBarTest extends JFrame {

        // Menu A
        private JMenuBar menubarA = new JMenuBar();
          // A Menu
          private JMenu menu1 = new JMenu("MenuA");

           // Menu A
        private JMenuBar menubarB = new JMenuBar();
          // A Menu
          private JMenu menu2 = new JMenu("MenuB");

        // Actions
        private Action switchMenuAction = new AbstractAction("Switch Menus") {
          public void actionPerformed(ActionEvent e) {
            switchMenuAction_actionPerformed();
          }
        };

        // Buttons
        JButton switchMenuButton = new JButton(switchMenuAction);

        public static void main(String[] args) {
          MenuBarTest test = new MenuBarTest();
          test.setVisible(true);
        }

        public void switchMenuAction_actionPerformed() {
          this.setJMenuBar(menubarB);
      // doesn't fix bug
      // this.repaint();
      // workaround for bug
      // SwingUtilities.updateComponentTreeUI(this);
        }

        public MenuBarTest() {
          this.getContentPane().add(switchMenuButton);
          this.setJMenuBar(menubarA);

          menubarA.add(menu1);
          menubarB.add(menu2);

          pack();
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The only way to get the menu to display is to call SwingUtilities.updateComponentTreeUI(this) in the JFrame after setting the JMenuBar (see http://www.jguru.com/forums/view.jsp?EID=1100274).
      (Incident Review ID: 191832)
      ======================================================================

            Unassigned Unassigned
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: