public TaskbarPositionTest() {
    super("Use CTRL-down to show a JPopupMenu");
    setContentPane(panel = createContentPane());
    setJMenuBar(createMenuBar("1 - First Menu", true));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // CTRL-down will show the popup.
    panel
        .getInputMap()
        .put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK), "OPEN_POPUP");
    panel.getActionMap().put("OPEN_POPUP", new PopupHandler());

    pack();

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    fullScreenBounds = new Rectangle(new Point(), toolkit.getScreenSize());
    screenBounds = new Rectangle(new Point(), toolkit.getScreenSize());

    // Place the frame near the bottom. This is a pretty wild guess.
    this.setLocation(0, (int) screenBounds.getHeight() - 2 * this.getHeight());

    // Reduce the screen bounds by the insets.
    GraphicsConfiguration gc = this.getGraphicsConfiguration();
    if (gc != null) {
      Insets screenInsets = toolkit.getScreenInsets(gc);
      screenBounds = gc.getBounds();
      screenBounds.width -= (screenInsets.left + screenInsets.right);
      screenBounds.height -= (screenInsets.top + screenInsets.bottom);
      screenBounds.x += screenInsets.left;
      screenBounds.y += screenInsets.top;
    }

    setVisible(true);
  }
示例#2
0
  /**
   * Returns an point which has been adjusted to take into account of the desktop bounds, taskbar
   * and multi-monitor configuration.
   *
   * <p>This adustment may be cancelled by invoking the application with
   * -Djavax.swing.adjustPopupLocationToFit=false
   */
  Point adjustPopupLocationToFitScreen(int xPosition, int yPosition) {
    Point popupLocation = new Point(xPosition, yPosition);

    if (popupPostionFixDisabled == true || GraphicsEnvironment.isHeadless()) {
      return popupLocation;
    }

    // Get screen bounds
    Rectangle scrBounds;
    GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation);
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    if (gc != null) {
      // If we have GraphicsConfiguration use it to get screen bounds
      scrBounds = gc.getBounds();
    } else {
      // If we don't have GraphicsConfiguration use primary screen
      scrBounds = new Rectangle(toolkit.getScreenSize());
    }

    // Calculate the screen size that popup should fit
    Dimension popupSize = JPopupMenu.this.getPreferredSize();
    long popupRightX = (long) popupLocation.x + (long) popupSize.width;
    long popupBottomY = (long) popupLocation.y + (long) popupSize.height;
    int scrWidth = scrBounds.width;
    int scrHeight = scrBounds.height;
    if (!canPopupOverlapTaskBar()) {
      // Insets include the task bar. Take them into account.
      Insets scrInsets = toolkit.getScreenInsets(gc);
      scrBounds.x += scrInsets.left;
      scrBounds.y += scrInsets.top;
      scrWidth -= scrInsets.left + scrInsets.right;
      scrHeight -= scrInsets.top + scrInsets.bottom;
    }
    int scrRightX = scrBounds.x + scrWidth;
    int scrBottomY = scrBounds.y + scrHeight;

    // Ensure that popup menu fits the screen
    if (popupRightX > (long) scrRightX) {
      popupLocation.x = scrRightX - popupSize.width;
      if (popupLocation.x < scrBounds.x) {
        popupLocation.x = scrBounds.x;
      }
    }
    if (popupBottomY > (long) scrBottomY) {
      popupLocation.y = scrBottomY - popupSize.height;
      if (popupLocation.y < scrBounds.y) {
        popupLocation.y = scrBounds.y;
      }
    }

    return popupLocation;
  }
示例#3
0
 /**
  * Tries to find GraphicsConfiguration that contains the mouse cursor position. Can return null.
  */
 private GraphicsConfiguration getCurrentGraphicsConfiguration(Point popupLocation) {
   GraphicsConfiguration gc = null;
   GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
   GraphicsDevice[] gd = ge.getScreenDevices();
   for (int i = 0; i < gd.length; i++) {
     if (gd[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
       GraphicsConfiguration dgc = gd[i].getDefaultConfiguration();
       if (dgc.getBounds().contains(popupLocation)) {
         gc = dgc;
         break;
       }
     }
   }
   // If not found and we have invoker, ask invoker about his gc
   if (gc == null && getInvoker() != null) {
     gc = getInvoker().getGraphicsConfiguration();
   }
   return gc;
 }