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

Hide Menu doesn't hide

XMLWordPrintable

    • beta2
    • generic
    • generic



      Name: ssT124754 Date: 01/10/2001


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)


      1. Compile and Run supplied snippet.
          Select The Bug -> Hide Menu (JMenuItem)
          THERE IS THE BUG
          Activate "Show Menu"
          Select The Bug -> Workaround (JMenuItem)
          and in second JFrame:
          Select Hide Menu (JMenuItem)
          AS IT SHOULD WORK.

      2. See end of this section
      3. No Error messages appear (only the display is not as it should be)
      4. Not Relavant.
      5. None
      **************** The source **********
      import javax.swing.JMenuBar;
      import javax.swing.JMenu;
      import javax.swing.JMenuItem;
      import javax.swing.JComponent;
      import javax.swing.JRootPane;
      import javax.swing.JButton;
      import java.awt.Container;
      import java.awt.LayoutManager;
      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Rectangle;
      import java.awt.Insets;
      import java.awt.event.ActionListener;
      import java.awt.event.ActionEvent;

      /**
        @author David M Gaskin (###@###.###) 5th Jan 2001
      */

      class MenuHideBug extends javax.swing.JFrame
         implements ActionListener {
         private boolean workaround = false;
         private static boolean initialWorkaround = false;
         JMenuItem hideMenu;
         JButton showMenu;
         JMenuBar mb;
         private MenuHideBug(String title) {
            super(title);
            workaround = initialWorkaround;
            showMenu = new JButton("Show Menu");
            showMenu.addActionListener(this);
            showMenu.setEnabled(false);
            showMenu.setBackground(Color.yellow);
            JComponent contentPane = (JComponent)getContentPane();
            contentPane.setBackground(Color.blue);
            contentPane.setPreferredSize(new Dimension(400, 300));
            contentPane.add("North", showMenu);
            mb = new JMenuBar();
            JMenu menu = null;
            JMenuItem showWorkaround = null;
            if (workaround) {
               menu = new JMenu("As it should work");
               setLocation(450, 350);
            }
            else {
               menu = new JMenu("The Bug");
               showWorkaround = new JMenuItem("Run Workaround");
               showWorkaround.addActionListener(this);
               menu.add(showWorkaround);
            }
            hideMenu = new JMenuItem("Hide Menu");
            hideMenu.addActionListener(this);
            menu.add(hideMenu);
            mb.add(menu);
            setJMenuBar(mb);
            pack();
            setVisible(true);
         }
         public void actionPerformed(ActionEvent ae) {
            Object source = ae.getSource();
            if (source == hideMenu) {
               if (workaround) {
                  setTitle("And now you don't (and SHOULDN'T)");
               }
               else
                  setTitle("Now you see it (and SHOULDN'T)");
               mb.setVisible(false);
               showMenu.setEnabled(true);
            }
            else if (source == showMenu) {
               mb.setVisible(true);
               showMenu.setEnabled(false);
            }
            else {
              initialWorkaround = true;
              new MenuHideBug("Now You don't");
            }
         }
         public JRootPane createRootPane() {
            JRootPane rc = null;
            if (initialWorkaround) {
               rc = new RootPane();
            }
            else
               rc = new JRootPane();
            return rc;
         }
         public static void main(String[] args) {
            new MenuHideBug("Now you see it (and you SHOULD)");
         }
      /* ************* WORK AROUND ***********************
         CUT, PASTED and CORRECTED from javax.swing.JRootPane
      *********************************************************/
         class RootPane extends javax.swing.JRootPane {
            protected LayoutManager createRootLayout() {
               return new CorrectedRootLayout();
            }
            protected class CorrectedRootLayout extends JRootPane.RootLayout {
              /**
               * Instructs the layout manager to perform the layout for
      the specified
               * container.
               *
               * @param the Container
      for which this layout manager is being used
               */
              public void
      layoutContainer(Container parent) {
                  Rectangle b =
      parent.getBounds();
                  Insets i = getInsets();
                  int contentY
      = 0;
                  int w = b.width - i.right - i.left;
                  int h =
      b.height - i.top - i.bottom;
              
                  if(layeredPane != null) {
          
                  layeredPane.setBounds(i.left, i.top, w, h);
                  }
                
        if(glassPane != null) {
                      glassPane.setBounds(i.left, i.top, w,
      h);
                  }
                  // Note: This is laying out the children in the
      layeredPane,
                  // technically, these are not our chilren.
      /*
      *********************** T H E E R R O R ********************************
         *********************** T H E E R R O R ********************************
                  if(menuBar != null) {

        PLEASE NOTE the same error exists in all of the other mentods of the super
        class!!!!!!!!!!!!!!!!!!!
         ***************************************************************************
      *****************************************************************************/*
                  if(menuBar != null && menuBar.isVisible()) { // <== SHOULD BE
                      Dimension mbd = menuBar.getPreferredSize();
                      
      menuBar.setBounds(0, 0, w, mbd.height);
                      contentY += mbd.height;
       
                 }
                  if(contentPane != null) {
                      
      contentPane.setBounds(0, contentY, w, h - contentY);
                  }
              }
         
         }
         }
      }
      (Review ID: 114636)
      ======================================================================

            mdavidsosunw Mark Davidson (Inactive)
            ssultanasunw Shaheen Sultana (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: