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

JMenu reports an error when switching to different window

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.5, 1.2.0
    • client-libs
    • None
    • generic, x86
    • generic, windows_nt

      I have a main application window, I want to invoke a 2nd window
      and hide the first main window. What happened was it reported the
      error message as follows:

      Received a mouse event from a non-showing Component com.sun.java.swing.JMenu[,0,
      1,39x19,layout=com.sun.java.swing.OverlayLayout, JMenu]

      I don't think this message should be printed.
      Please advise me ASAP if there is a work-around. Thanks!

      I have stripped down my code to two simple files: MainFrame.java & ResultWindow.java
      You may compile them to see the error.


      // MainFrame.java --------------------------------------------------------------------------
      import java.awt.*;
      import java.util.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;

      /**
       * The application main entry class
       */
      public class MainFrame extends JFrame
      {
          static ResultWindow jResultWindow; // the result window for displaying data
          static MainFrame jMainAppFrame; // application's main frame

       JMenuBar mainMenuBar;
       JMenu menuFile;
       JMenuItem miOpen;
       JMenuItem miViewResult;
       JMenuItem miExit;

          /** Constructor **/
       public MainFrame()
       {

        mainMenuBar = new JMenuBar();
        menuFile = MainFrame.addMenu(mainMenuBar, "File", 'F');
                      miViewResult = MainFrame.addMenuItem(menuFile, "View Result Window", 'R');
        menuFile.addSeparator();
        miExit = MainFrame.addMenuItem(menuFile, "Exit", 'X');

        setJMenuBar(mainMenuBar);

              SymAction lSymAction = new SymAction();
        miViewResult.addActionListener(lSymAction);
        miExit.addActionListener(lSymAction);
       }

       public MainFrame(String title)
       {
        this();
        setTitle(title);
       }

          /**
           * Call this method to center the window in the screen
           */
          static void centerWindow(java.awt.Window frame)
          {
              //Center the window
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              Dimension frameSize = frame.getSize();
              if (frameSize.height > screenSize.height)
                frameSize.height = screenSize.height;
              if (frameSize.width > screenSize.width)
                frameSize.width = screenSize.width;
              frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
          }

       public static void main(String args[])
       {
              // create the MainFrame
              jMainAppFrame = new MainFrame();

              // Center the Frame window and display it
           jMainAppFrame.setSize(450,320);
           MainFrame.centerWindow(jMainAppFrame);
              jMainAppFrame.setVisible(true);
       }

       /**
        * Creates and displays the Result Window and hides the Main Window.
        */
          public static synchronized ResultWindow createResultWindow(boolean bViewOnly)
          {
              // display wait cursor while canceling test
      // Cursor oldCursor = getCursor();
      // setCursor(new Cursor(WAIT_CURSOR));

              // create the result window
              jResultWindow = new ResultWindow(bViewOnly);

              jResultWindow.setLocation(jMainAppFrame.getLocation());
              jResultWindow.setVisible(true);
              jMainAppFrame.setVisible(false);

      // setCursor(oldCursor);

        return jResultWindow;
          }

       class SymAction implements java.awt.event.ActionListener
       {
        public void actionPerformed(java.awt.event.ActionEvent event)
        {
         Object object = event.getSource();
         if (object == miViewResult) {
             createResultWindow(true);
         }
         else if (object == miExit)
          miExit_Action(event);
           }
       }

       void miExit_Action(java.awt.event.ActionEvent event)
       {
           setVisible(false); // hide the Frame
        dispose(); // free the system resources
        System.exit(0); // close the application
       }

       public static JMenu addMenu(JMenuBar parent, String menuname, char mnemonic)
       {
        JMenu menu = new JMenu(menuname);
        menu.setMnemonic(mnemonic);
        parent.add(menu);
        return menu;
       }

       public static JMenuItem addMenuItem(JMenu parent, String menuname, char mnemonic)
       {
        JMenuItem menu = new JMenuItem(menuname);
        menu.setMnemonic(mnemonic);
        parent.add(menu);
        return menu;
       }
      }



      // ResultWindow.java ------------------------------------------------------------------------------------

      import java.awt.*;
      import java.io.*;
      import java.util.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;
      import com.sun.java.swing.plaf.basic.*;

      public class ResultWindow extends JFrame
      {
          final String APP_RESULT_WIN_STR = "Result Window";
       boolean jIsViewOnly;
       JMenuBar mainMenuBar;
       JMenu menuFile;
       JMenuItem miClose;

          ResultWindow(boolean bViewOnly)
          {
        jIsViewOnly = bViewOnly;

              // set JFrame's layout manager
              getContentPane().setLayout(new BorderLayout());

        mainMenuBar = new JMenuBar();
        menuFile = MainFrame.addMenu(mainMenuBar, "File", 'F');
        miClose = MainFrame.addMenuItem(menuFile, "Close", 'C');
        setJMenuBar(mainMenuBar);

              setTitle(APP_RESULT_WIN_STR);
              setSize(550,350);
              setLocation(300,200);

        SymAction lSymAction = new SymAction();
        miClose.addActionListener(lSymAction);
          }

       class SymAction implements java.awt.event.ActionListener
       {
        public void actionPerformed(java.awt.event.ActionEvent event)
        {
         Object object = event.getSource();
         if (object == miClose)
          miClose_Action(event);
           }
       }

          void miClose_Action(java.awt.event.ActionEvent event)
       {
              closeWindow();
       }

          void closeWindow() {
              setVisible(false);
           dispose(); // free the system resources
              MainFrame.jMainAppFrame.setVisible(true);
          }
      }

            gsaab Georges Saab
            rkarsunw Ralph Kar (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: