title: Destroy a JPopupMenu in Java Swing
body: <p>I'm using a JPopupMenu displayed when I do a right click on certain components in my GUI. </p>
  
  <p>Now I have to destroy the popup menu displayed in this 2 situation:</p>
  
  <ol>
  <li>The user click on a menu entry displayed into the popup (do the related action and close the popup)</li>
  <li>The user click somewhere else on the screen(close the popup without do anything)</li>
  </ol>
  
  <p>I solved this problem storing into an ArrayList the current visible popups and I manually set them to be invisible when one of the 2 previous situation occured.</p>
  
  <p>So, i would like to know 2 things:</p>
  
  <ol>
  <li>Is there any cleaner way of doing that without manually taking the reference of all active popups? (perhaps any Swing feature do accomplish that? )</li>
  <li>Is just enough to set a popup unvisible having no more references to that object, in order to free its allocated memory? Or have I to use a method like dispose ? (there isn't a dispose method defined in JPopupMenu)</li>
  </ol>
  
  <p>It is a bit difficult to show my actual code, because it's a bit complex. Anyway it does the following:</p>
  
  <pre><code>public EditorPopupMenu getPopupMenu() {
      this.popupMenu = new EditorPopupMenu();
  
      EditorMenuItem copy = GuiConcreteFactory.getInstance().createMenuItem(Gui.getInstance().copyItemAction);
      EditorMenuItem cut = GuiConcreteFactory.getInstance().createMenuItem(Gui.getInstance().cutItemAction);
      EditorMenuItem paste = GuiConcreteFactory.getInstance().createMenuItem(Gui.getInstance().pasteItemAction);
  
      this.popupMenu.add(copy);
      this.popupMenu.add(cut);
      this.popupMenu.add(paste);
  
      this.popupMenu.addSeparator();
  
      EditorMenuItem settings = GuiConcreteFactory.getInstance().createMenuItem(
                                                  new ApplicationShowDialogAction("settings",null, 
                                                          new EditorAreaDialog (this)) );
      this.popupMenu.add(settings);
      return popupMenu;
  }
  </code></pre>
  
  <p>Where EditorPopupMenu extends JPopupMenu. Previous code is called by a MouseListener when a click happend on a particular object and the specified object constructs its popup menu and returns it.</p>
  
  <p>From inside the MouseListener:</p>
  
  <pre><code>if (me.getModifiers() == InputEvent.BUTTON3_MASK){
                  // //System.out.println("ResizableMouseAdapter: BUTTON_3_MASK");
  
  
                   EditorPopupMenu popupMenu = sourceComp.getType().getPopupMenu();
                   if ( popupMenu!= null){
                       //System.out.println("COMPONENT HAS A POPUP MENU");
                       popupMenu.setLocation( sourceComp.getLocationOnScreen().x + me.getX(),
                                              sourceComp.getLocationOnScreen().y + me.getY());
                       popupMenu.setVisible(true);
                       Gui.getInstance().addActivePopup(popupMenu);
                   }
  
               }
  </code></pre>
  
  <p>This is all. With this code my JPopupMenu doesn't dissapear properly.</p>