Container rootContainer = (Container) SwingUtilities.getRoot(invoker); Dimension screenSize = getToolkit().getScreenSize();
  public void setVisible(boolean visible)  {    if (visible == isVisible())      return;    boolean old = isVisible();    this.visible = visible;    if (old != isVisible())      {        firePropertyChange("visible", old, isVisible());        if (visible)          {            firePopupMenuWillBecomeVisible();            Container rootContainer = (Container) SwingUtilities.getRoot(invoker);            Dimension screenSize = getToolkit().getScreenSize();                        boolean fit = true;            Dimension size = getSize();            if (size.width == 0 && size.height == 0)              {                size = getPreferredSize();                setSize(size);              }            if ((size.width > (rootContainer.getWidth() - popupLocation.x))                || (size.height > (rootContainer.getHeight() - popupLocation.y)))              fit = false;            if (lightWeightPopupEnabled && fit)              popup = new LightWeightPopup(this);            else              {                if (fit)                  popup = new MediumWeightPopup(this);                else                  {                    popup = new HeavyWeightPopup(this);                    setLightWeightPopupEnabled(false);                  }              }            if (popup instanceof LightWeightPopup                || popup instanceof MediumWeightPopup)              {                JLayeredPane layeredPane;                layeredPane = SwingUtilities.getRootPane(invoker).getLayeredPane();                Point p = new Point(popupLocation.x, popupLocation.y);                                if (layeredPane.isShowing())                  SwingUtilities.convertPointFromScreen(p, layeredPane);                                          if (size.width + popupLocation.x > screenSize.width)                  popupLocation.x -= size.width;                if (size.height + popupLocation.y > screenSize.height)                  popupLocation.y -= size.height;                popup.show(p.x, p.y, size.width, size.height);              }            else              {                // Subtract insets of the top-level container if popup menu's                // top-left corner is inside it.                Insets insets = rootContainer.getInsets();                if (size.width + popupLocation.x > screenSize.width)                  popupLocation.x -= size.width;                if (size.height + popupLocation.y > screenSize.height)                  popupLocation.y -= size.height;                                  popup.show(popupLocation.x - insets.left,                            popupLocation.y - insets.top,                           size.width, size.height);              }          }        else          {            firePopupMenuWillBecomeInvisible();            popup.hide();          }      }  }