예제 #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);
  }
예제 #2
0
    @Override
    public Dimension getPreferredSize() {
      final Component focusOwner =
          KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
      Point p = null;
      if (focusOwner != null && focusOwner.isShowing()) {
        p = focusOwner.getLocationOnScreen();
      }

      return computeNotBiggerDimension(super.getPreferredSize().getSize(), p);
    }