Ejemplo n.º 1
0
  @Override
  public void mouseMoved(MouseEvent e) {
    final MouseEvent event = SwingUtilities.convertMouseEvent(e.getComponent(), e, getParent());
    final boolean insideRec = getBounds().contains(event.getPoint());
    boolean buttonsNotPressed =
        (e.getModifiersEx()
                & (InputEvent.BUTTON1_DOWN_MASK
                    | InputEvent.BUTTON2_DOWN_MASK
                    | InputEvent.BUTTON3_DOWN_MASK))
            == 0;
    if (!myPopupIsShowing && insideRec && buttonsNotPressed) {
      showPopup(null, false);
    } else if (myPopupIsShowing && !insideRec) {
      final Component over =
          SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY());
      JPopupMenu popup = myUnderPopup.isShowing() ? myUnderPopup : myAbovePopup;
      if (over != null && popup.isShowing()) {
        final Rectangle rec = new Rectangle(popup.getLocationOnScreen(), popup.getSize());
        int delta = 15;
        rec.x -= delta;
        rec.width += delta * 2;
        rec.y -= delta;
        rec.height += delta * 2;

        final Point eventPoint = e.getPoint();
        SwingUtilities.convertPointToScreen(eventPoint, e.getComponent());

        if (rec.contains(eventPoint)) {
          return;
        }
      }

      closePopup();
    }
  }
Ejemplo n.º 2
0
  public static void main(String... args) throws Exception {
    Robot robot = new Robot();

    SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            popup = new JPopupMenu();
            popup.add(new JMenuItem("item"));
            popup.setVisible(true);
          }
        });

    robot.waitForIdle();

    if (!popup.isShowing()) {
      throw new RuntimeException("Where is my popup ?");
    }

    SwingUtilities.invokeAndWait(
        new Runnable() {
          public void run() {
            popup.setVisible(false);
            popup.removeAll();
            popup.setVisible(true);
          }
        });

    robot.waitForIdle();

    if (popup.isShowing()) {
      throw new RuntimeException("Empty popup is shown");
    }

    popup.setVisible(false);
  }