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

JPopupMenus and JMenus persist when their JFrame becomes visible again.

XMLWordPrintable



      Name: skT88420 Date: 05/04/99


      There seems to be a problem with JPopupMenus and JMenus when they are contained in a JFrame
      that is set invisible and then becomes visible again. A sample program is included to
      demonstrate the problem. Here are steps to see the problem:
      1. click on the login button on the login window
      2. click on the "Close in 5 sec" button (this starts a "thread safe" thread that will close
      the main window in 5 seconds)
      3. before 5 seconds has elapsed, click on the JLable named "Popup Menu" on the left side of
      the main window so that the popup menu pops up.
      4. wait 5 seconds
      5. re-login with the login button
      6. notice that the popup menu is still there (in fact you can't get rid of it)
      Something similar happens to the JMenus. To see that replace step 3 above with a click on
      the JMenuBar at the top of the main Window. You will notice that when you log back in the
      JMenu is still there and won't go away easily. It will go away if you click on the JMenu
      title again, but clicks off of the JMenu, which normally makes it go away, won't work.

      java full version "JDK1.1.7:12/16/1998-19:30"

      Here is the sample program code:

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;

      public class PopupMenuTest extends JFrame
         implements ActionListener, MouseListener, Runnable
      {
         // Constants
         
         private static final String button1String = "Login";
         private static final String button2String = "Close in 5 sec";
         private static final String menuString1 = "Menu Choice 1";
         private static final String menuString2 = "Menu Choice 2";
         private static final String menuString3 = "Menu Choice 3";
         private static final String menuString4 = "Menu Choice 4";
         private static final String label1String = "Popup Menu";

         // Variables
         
         private JFrame mainWindow;
         
         private JPopupMenu mainPopupMenu;
         
         private JMenu mainMenu;
         private JMenu mainMenu2;
         
         private JMenuBar menuBar;

         private JButton button1;
         private JButton button2;

         private JLabel label1;

         // Constructor
         
         public PopupMenuTest()
         {
            setTitle("Login");
            
            button1 = new JButton(button1String);
            
            getContentPane().add("Center", button1);
            
            button1.addActionListener(this);
            
            mainWindow = new JFrame("Main Window");
            
            mainWindow.setSize(new Dimension(500, 300));
            
            button2 = new JButton(button2String);
            
            mainWindow.getContentPane().add("South", button2);
            
            button2.addActionListener(this);
            
            label1 = new JLabel(label1String);
            
            mainWindow.getContentPane().add("West", label1);
            
            label1.addMouseListener(this);
            
            mainMenu = new JMenu("Main Menu");
            
            mainMenu.add(menuString1);
            mainMenu.add(menuString2);
            mainMenu.add(menuString3);
            mainMenu.add(menuString4);

            mainMenu2 = new JMenu("Menu2");
            
            mainMenu2.add(menuString1);
            mainMenu2.add(menuString2);
            mainMenu2.add(menuString3);
            mainMenu2.add(menuString4);

            menuBar = new JMenuBar();
            mainWindow.setJMenuBar(menuBar);
            
            menuBar.add(mainMenu);
            menuBar.add(mainMenu2);
               
            pack();
            
            WindowAdapter wA = new WindowAdapter()
            {
               public void windowClosing(WindowEvent evt)
               {
                  System.exit(0);
               }
            };
            
            addWindowListener(wA);
            mainWindow.addWindowListener(wA);
            
            setVisible(true);
            
         }
         
         // Methods
         
         // create the popup menu
         public void createPopup(Component comp, Point point)
         {
            mainPopupMenu = new JPopupMenu();
            
            mainPopupMenu.add(menuString1);
            mainPopupMenu.add(menuString2);
            mainPopupMenu.add(menuString3);
            mainPopupMenu.add(menuString4);
            
            mainPopupMenu.show(comp, point.x, point.y);
         
         
         }
         
         // ActionListener interface
         public void actionPerformed(ActionEvent evt)
         {
            // login button was pressed
            if(evt.getActionCommand().equals(button1String))
            {
               setVisible(false);
               mainWindow.setVisible(true);
            }
            else if(evt.getActionCommand().equals(button2String))
            {
               Thread tempThread = new Thread(this);

               tempThread.start();
            }
         }
         
         // MouseListener interface
         
         public void mouseClicked(MouseEvent evt)
         {
            createPopup(evt.getComponent(), evt.getPoint());
            
         }
         
         public void mousePressed(MouseEvent evt)
         {}
         
         public void mouseReleased(MouseEvent evt)
         {}
         
         public void mouseEntered(MouseEvent evt)
         {}
         
         public void mouseExited(MouseEvent evt)
         {}
         
         // Runnable interface
         // a seperate thread to set mainwindow to invisible
         public void run()
         {
            try
            {
               Thread.sleep(5000);
            }
            catch(java.lang.InterruptedException e)
            {
            }
            
            // invoke the actions in the event thread for "Thread Safeness"
            SwingUtilities.invokeLater(new Runnable()
            {
               public void run()
               {
                  mainWindow.setVisible(false);
                  setVisible(true);
               }
            });

         }
         
         
         public static void main(String args[])
         {
            new PopupMenuTest();
         }
           
      }
      (Review ID: 57786)
      ======================================================================

            peterz Peter Zhelezniakov
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: