Example #1
0
  /**
   * Displays the popup menu at the position x,y in the coordinate space of the component invoker.
   *
   * @param invoker the component in whose space the popup menu is to appear
   * @param x the x coordinate in invoker's coordinate space at which the popup menu is to be
   *     displayed
   * @param y the y coordinate in invoker's coordinate space at which the popup menu is to be
   *     displayed
   */
  public void show(Component invoker, int x, int y) {
    if (DEBUG) {
      System.out.println("in JPopupMenu.show ");
    }
    setInvoker(invoker);
    Frame newFrame = getFrame(invoker);
    if (newFrame != frame) {
      // Use the invoker's frame so that events
      // are propagated properly
      if (newFrame != null) {
        this.frame = newFrame;
        if (popup != null) {
          setVisible(false);
        }
      }
    }
    Point invokerOrigin;
    if (invoker != null) {
      invokerOrigin = invoker.getLocationOnScreen();

      // To avoid integer overflow
      long lx, ly;
      lx = ((long) invokerOrigin.x) + ((long) x);
      ly = ((long) invokerOrigin.y) + ((long) y);
      if (lx > Integer.MAX_VALUE) lx = Integer.MAX_VALUE;
      if (lx < Integer.MIN_VALUE) lx = Integer.MIN_VALUE;
      if (ly > Integer.MAX_VALUE) ly = Integer.MAX_VALUE;
      if (ly < Integer.MIN_VALUE) ly = Integer.MIN_VALUE;

      setLocation((int) lx, (int) ly);
    } else {
      setLocation(x, y);
    }
    setVisible(true);
  }
  protected void customizePopup(BasicComboPopup popup) {
    scrollPane = getScrollPane(popup);

    if (popupWider) popupWider(popup);

    checkHorizontalScrollBar(popup);

    //  For some reason in JDK7 the popup will not display at its preferred
    //  width unless its location has been changed from its default
    //  (ie. for normal "pop down" shift the popup and reset)

    Component comboBox = popup.getInvoker();
    Point location = comboBox.getLocationOnScreen();

    if (popupAbove) {
      int height = popup.getPreferredSize().height;
      popup.setLocation(location.x, location.y - height);
    } else {
      int height = comboBox.getPreferredSize().height;
      popup.setLocation(location.x, location.y + height - 1);
      popup.setLocation(location.x, location.y + height);
    }
  }